4 |
|
""" |
5 |
|
|
6 |
|
from crab_exceptions import * |
7 |
< |
from crab_util import runCommand, UnixUserName |
7 |
> |
from crab_util import runCommand, getUserName |
8 |
|
import common |
9 |
|
import os, string, time |
10 |
– |
from ProdCommon.SiteDB.SiteDB import SiteDBJSON |
10 |
|
|
11 |
|
|
12 |
< |
def LFNBase(PrimaryDataset='',ProcessedDataset='',merged=True,LocalUser=False,publish=False): |
12 |
> |
def LFNBase(forced_path, PrimaryDataset='',ProcessedDataset='',merged=True,publish=False): |
13 |
|
""" |
14 |
|
""" |
16 |
– |
lfnbase = "/store" |
17 |
– |
if not merged: |
18 |
– |
lfnbase = os.path.join(lfnbase,"tmp") |
15 |
|
if (PrimaryDataset == 'null'): |
16 |
|
PrimaryDataset = ProcessedDataset |
17 |
< |
#lfnbase = os.path.join(lfnbase, "user", getUserName(LocalUser=LocalUser), PrimaryDataset, ProcessedDataset ) |
18 |
< |
### for tutorial |
19 |
< |
lfnbase = os.path.join(lfnbase, "user", getUserName(LocalUser=LocalUser), PrimaryDataset, ProcessedDataset ) |
20 |
< |
|
17 |
> |
if PrimaryDataset != '': |
18 |
> |
if ( PrimaryDataset[0] == '/' ): PrimaryDataset=PrimaryDataset[1:] |
19 |
> |
if forced_path.find('store/group')>0: |
20 |
> |
lfnbase = os.path.join(forced_path, PrimaryDataset, ProcessedDataset) |
21 |
> |
else: |
22 |
> |
lfnbase = os.path.join(forced_path, getUserName(), PrimaryDataset, ProcessedDataset) |
23 |
> |
if (publish == True): |
24 |
> |
checkSlash(ProcessedDataset) |
25 |
> |
checkLength(lfnbase, forced_path, PrimaryDataset, ProcessedDataset) |
26 |
|
return lfnbase |
27 |
|
|
28 |
< |
def getUnixUserName(): |
28 |
< |
""" |
29 |
< |
extract username from whoami |
28 |
> |
def checkSlash(ProcessedDataset): |
29 |
|
""" |
30 |
< |
return UnixUserName() |
32 |
< |
|
33 |
< |
def getDN(): |
30 |
> |
check if [USER].publish_data_name contains not allowed slash |
31 |
|
""" |
32 |
< |
extract DN from user proxy's identity |
33 |
< |
""" |
34 |
< |
try: |
35 |
< |
userdn = runCommand("voms-proxy-info -identity") |
36 |
< |
userdn = string.strip(userdn) |
37 |
< |
#search for a / to avoid picking up warning messages |
38 |
< |
userdn = userdn[userdn.find('/'):] |
39 |
< |
except: |
40 |
< |
msg = "Error. Problem with voms-proxy-info -identity command" |
41 |
< |
raise CrabException(msg) |
42 |
< |
return userdn.split('\n')[0] |
43 |
< |
|
44 |
< |
def gethnUserNameFromSiteDB(): |
45 |
< |
""" |
46 |
< |
extract user name from SiteDB |
47 |
< |
""" |
48 |
< |
hnUserName = None |
49 |
< |
userdn = getDN() |
50 |
< |
mySiteDB = SiteDBJSON() |
51 |
< |
try: |
52 |
< |
hnUserName = mySiteDB.dnUserName(dn=userdn) |
53 |
< |
except: |
54 |
< |
msg = "Error. Problem extracting user name from SiteDB" |
55 |
< |
msg += "\n Check that you are registered in SiteDB, see https://twiki.cern.ch/twiki/bin/view/CMS/SiteDBForCRAB" |
56 |
< |
raise CrabException(msg) |
57 |
< |
if not hnUserName: |
58 |
< |
msg = "Error. There is no user name associated to DN %s in SiteDB. You need to register in SiteDB with the instructions at https://twiki.cern.ch/twiki/bin/view/CMS/SiteDBForCRAB" % userdn |
59 |
< |
print msg |
60 |
< |
raise CrabException(msg) |
61 |
< |
return hnUserName |
62 |
< |
|
63 |
< |
|
64 |
< |
def gethnUserName(): |
65 |
< |
""" |
66 |
< |
cache the username extracted from SiteDB for 24hours |
67 |
< |
""" |
68 |
< |
userconfigFileName="SiteDBusername.conf" |
69 |
< |
if not os.path.exists(userconfigFileName): |
70 |
< |
common.logger.debug(5,"Downloading from SiteDB username into %s"%userconfigFileName) |
71 |
< |
nameuser = gethnUserNameFromSiteDB() |
72 |
< |
userfile = open(userconfigFileName, 'w') |
73 |
< |
userfile.write(nameuser) |
74 |
< |
userfile.close() |
75 |
< |
else: |
76 |
< |
statinfo = os.stat(userconfigFileName) |
77 |
< |
## if the file is older then 24 hours it is re-downloaded to update the configuration |
78 |
< |
oldness = 24*3600 |
82 |
< |
if (time.time() - statinfo.st_ctime) > oldness: |
83 |
< |
common.logger.debug(5,"Downloading from SiteDB username into %s"%userconfigFileName) |
84 |
< |
nameuser = gethnUserNameFromSiteDB() |
85 |
< |
userfile = open(userconfigFileName, 'w') |
86 |
< |
userfile.write(nameuser) |
87 |
< |
userfile.close() |
32 |
> |
import re |
33 |
> |
|
34 |
> |
#if (str(ProcessedDataset).find("/") > -1): |
35 |
> |
# raise CrabException("Warning: [USER] publication_data_name contains a '/' characters that is not allowed. Please change the publication_data_name value") |
36 |
> |
m = re.findall("[^-\w_\.%]+", str(ProcessedDataset)) |
37 |
> |
if (m != []): |
38 |
> |
common.logger.debug("check not allowed characters in the publication_data_name = " + str(m)) |
39 |
> |
raise CrabException("Warning: [USER] publication_data_name contains not allowed characters %s . Please change the publication_data_name value" % str(m)) |
40 |
> |
|
41 |
> |
def checkLength(lfnbase, forced_path, PrimaryDataset, ProcessedDataset): |
42 |
> |
""" |
43 |
> |
""" |
44 |
> |
|
45 |
> |
len_primary = len(PrimaryDataset) |
46 |
> |
|
47 |
> |
common.logger.debug("CheckLength of LFN and User DatasetName") |
48 |
> |
common.logger.debug("max length for complete LFN is 500 characters, max length for primary dataset is 100 characters") |
49 |
> |
common.logger.debug("PrimaryDataset = " + PrimaryDataset) |
50 |
> |
common.logger.debug("len_primary = " + str(len_primary)) |
51 |
> |
|
52 |
> |
if (len_primary > 100): |
53 |
> |
raise CrabException("Warning: you required user_data_publication. The PrimaryDatasetName has to be < 100 characters") |
54 |
> |
|
55 |
> |
if (PrimaryDataset != ProcessedDataset): |
56 |
> |
common.logger.debug("ProcessedDataset = " + ProcessedDataset) |
57 |
> |
common.logger.debug("len(ProcessedDataset) = " + str(len(ProcessedDataset))) |
58 |
> |
|
59 |
> |
common.logger.debug("forced_path = " + forced_path) |
60 |
> |
common.logger.debug("len(forced_path) = " + str(len(forced_path))) |
61 |
> |
|
62 |
> |
user = getUserName() |
63 |
> |
len_user_name = len(user) |
64 |
> |
common.logger.debug("user = " + user) |
65 |
> |
common.logger.debug("len_user_name = " + str(len_user_name)) |
66 |
> |
|
67 |
> |
common.logger.debug("lfnbase = " + lfnbase) |
68 |
> |
common.logger.debug("len(lfnbase) = " + str(len(lfnbase))) |
69 |
> |
|
70 |
> |
### we suppose a output_file_name of 50 characters ### |
71 |
> |
if len(lfnbase)>450: |
72 |
> |
if (PrimaryDataset != ProcessedDataset): |
73 |
> |
#500 - len_user_name - len_primary - len(forced) - len(PSETHASH = 32) - 4(/) - output(50) |
74 |
> |
#~400 - len_user_name - len_primary - len(forced) |
75 |
> |
if (len(ProcessedDataset) > (400 - len_user_name - len_primary - len(forced_path))): |
76 |
> |
raise CrabException("Warning: publication name too long. USER.publish_data_name has to be < " + str(400 - len_user_name - len_primary - len(forced_path)) + " characters") |
77 |
> |
else: |
78 |
> |
raise CrabException("Warning: LFN > 500 characters") |
79 |
|
else: |
80 |
< |
userfile = open(userconfigFileName, 'r') |
81 |
< |
for line in userfile.readlines(): |
82 |
< |
nameuser = line |
83 |
< |
userfile.close() |
84 |
< |
nameuser = string.strip(nameuser) |
94 |
< |
return nameuser |
95 |
< |
|
96 |
< |
def getUserName(LocalUser=False): |
97 |
< |
""" |
98 |
< |
extract user name from either SiteDB or Unix |
99 |
< |
""" |
100 |
< |
if LocalUser: |
101 |
< |
common.logger.debug(10,"Using as username the Unix user name") |
102 |
< |
UserName=getUnixUserName() |
103 |
< |
return UserName |
104 |
< |
|
105 |
< |
UserName=gethnUserName() |
106 |
< |
return UserName |
80 |
> |
if (len(ProcessedDataset) > (400 - len_user_name - len(forced_path)) / 2): |
81 |
> |
raise CrabException("Warning: publication name too long. USER.publish_data_name has to be < " + str((400 - len_user_name - len(forced_path)) / 2) + " characters") |
82 |
> |
else: |
83 |
> |
raise CrabException("Warning: LFN > 500 characters") |
84 |
> |
|
85 |
|
|
86 |
|
if __name__ == '__main__' : |
87 |
|
""" |
88 |
|
""" |
89 |
< |
from crab_logger import Logger |
90 |
< |
from WorkSpace import * |
113 |
< |
continue_dir = os.path.expanduser("~") |
114 |
< |
cfg_params={'USER.logdir' : continue_dir } |
115 |
< |
common.work_space = WorkSpace(continue_dir, cfg_params) |
116 |
< |
log = Logger() |
117 |
< |
common.logger = log |
89 |
> |
import logging |
90 |
> |
common.logger = logging |
91 |
|
|
92 |
|
print "xx %s xx"%getUserName() |
93 |
|
baselfn = LFNBase("datasetstring") |