ATLAS Offline Software
pool_uuid.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4 
5 import sys
6 import datetime, time
7 import uuid
8 
9 version_str = ('Unused', 'Gregorian time based', 'DCE', 'MD5 based', 'RNG based', 'SHA',
10  'Reordered Gregorian', 'Epoch time based', 'Custom') + ('Reserved',)*7
11 
12 def uuid_to_ts( myuuid ) :
13  # from web.archive.org/web/20110817134817/http://www.gossamer-threads.com:80/lists/python/dev/670680
14  DTD_DELTA = ( datetime.datetime( *time.gmtime(0)[0:3] ) -
15  datetime.datetime( 1582, 10, 15 ) )
16  DTD_SECS_DELTA = DTD_DELTA.days * 86400 # 0x01b21dd213814000/1e7
17  secs = myuuid.time / 1e7
18  secs_epoch = secs - DTD_SECS_DELTA
19  return datetime.datetime.fromtimestamp( secs_epoch )
20 
21 def uuid_split( guid ):
22  fields = guid.split('-')
23  if len(fields) != 5: raise ValueError( f"Wrong number of fields in '{guid}'" )
24  if len(fields[0]) != 8: raise ValueError( f"Wrong length of field#0: '{fields[0]}'" )
25  if len(fields[1]) != 4: raise ValueError( f"Wrong length of field#1: '{fields[1]}'" )
26  if len(fields[2]) != 4: raise ValueError( f"Wrong length of field#2: '{fields[2]}'" )
27  if len(fields[3]) != 4: raise ValueError( f"Wrong length of field#3: '{fields[3]}'" )
28  if len(fields[4]) != 12: raise ValueError( f"Wrong length of field#4: '{fields[4]}'" )
29  return (fields[0], fields[1], fields[2], fields[3], fields[4])
30 
31 # DDCCBBAA-FFEE-HHGG-IIJJ-KKLLMMNNOOPP to AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP
32 def pool_to_uuid( mypool ) :
33  print('POOL guid:', mypool)
34  f0, f1, f2, f3, f4 = uuid_split(mypool)
35  myuuid = '%s%s%s%s-%s%s-%s%s-%s-%s'\
36  %(f0[6:8],f0[4:6],f0[2:4],f0[0:2],\
37  f1[2:4],f1[0:2],f2[2:4],f2[0:2],f3,f4)
38  print('UUID guid:', myuuid)
39  return myuuid
40 
41 # AABBCCDD-EEFF-GHIJ-xxxx-xxxxxxxxxxxx to HIJEEFFAABBCCDD
42 def timorder_uuid( myuuid ) :
43  f0, f1, f2, f3, f4 = uuid_split(myuuid)
44  myuuidto = '%s%s%s'%(f2[1:4],f1,f0)
45  myUUID=uuid.UUID(myuuid)
46  print('UUID:', myUUID)
47  print('UUID time (100ns intervals since 15 Oct 1582):', myUUID.time, "hex:", hex(myUUID.time))
48  print('UUID timeordered:', myuuidto)
49  print('UUID timestamp:', uuid_to_ts(myUUID))
50  return myuuidto
51 
52 # DDCCBBAA-FFEE-IJGH-xxxx-xxxxxxxxxxxx to HIJEEFFAABBCCDD
53 def timorder_pool( mypool ) :
54  print('POOL guid:', mypool)
55  f0, f1, f2, f3, f4 = uuid_split(mypool)
56  myuuidto = '%s%s%s%s%s%s%s%s'\
57  %(f2[2:3],f2[0:2],f1[2:4],f1[0:2],\
58  f0[6:8],f0[4:6],f0[2:4],f0[0:2],)
59  print('UUID timeordered:', myuuidto)
60  return myuuidto
61 
62 
63 def printuuid( myuuid ) :
64  myUUID=uuid.UUID(myuuid)
65  print('UUID:', myUUID)
66  print('UUID variant:', myUUID.variant)
67  print('UUID version:', myUUID.version, '-',version_str[myUUID.version])
68  print('UUID field#0 (time_low): %12.8x (%12.8x)'\
69  %( myUUID.fields[0], myUUID.time_low ))
70  print('UUID field#1 (time_mid): %12.4x (%12.4x)'\
71  %( myUUID.fields[1], myUUID.time_mid ))
72  print('UUID field#2 (time_hi_version): %12.4x (%12.4x)'\
73  %( myUUID.fields[2], myUUID.time_hi_version ))
74  print('UUID field#3 (clock_seq_hi_variant): %12.2x (%12.2x)'\
75  %( myUUID.fields[3], myUUID.clock_seq_hi_variant ))
76  print('UUID field#4 (clock_seq_low): %12.2x (%12.2x)'\
77  %( myUUID.fields[4], myUUID.clock_seq_low ))
78  print('UUID field#5 (node): %12.12x (%12.12x)'\
79  %( myUUID.fields[5], myUUID.node ))
80  if myUUID.version == 1:
81  timorder_uuid( myuuid )
82  print('================================================')
83 
84 def usage():
85  print(sys.argv[0],"-h | [-u] UUID")
86  print(" -u : assume uuidgen standard format (not POOL)")
87  sys.exit(0)
88 
89 def example():
90  # [avalassi@lxplus248 tcsh] ~ > date ; pool_gen_uuid ; uuidgen -t
91  # A2E338DD-2265-DF11-AFE1-001E4F3E5C33
92  # dd3a5aac-6522-11df-82eb-001e4f3e5c33
93 
94  guid1='dd3a5aac-6522-11df-82eb-001e4f3e5c33' # from uuidgen
95  guid2='A2E338DD-2265-DF11-AFE1-001E4F3E5C33' # from POOL
96  guid3='A2E338DD-2265-D011-AFE1-001E4F3E5C33' # POOL token for the year 1997
97  printuuid( guid1 )
98  printuuid( pool_to_uuid( guid2 ) )
99  printuuid( pool_to_uuid( guid3 ) )
100 
101 
102 if __name__ == "__main__":
103  regular = False
104  if len(sys.argv) > 1:
105  if sys.argv[1] == '-u':
106  sys.argv.pop(1)
107  regular = True
108  if sys.argv[1] == '-h':
109  usage()
110  if len(sys.argv) < 2:
111  usage()
112  guid = sys.argv[1]
113  if not regular:
114  # assume POOL format
115  guid = pool_to_uuid( guid )
116  printuuid( guid )
117  else:
118  example()
119 
pool_uuid.uuid_to_ts
def uuid_to_ts(myuuid)
Definition: pool_uuid.py:12
pool_uuid.timorder_pool
def timorder_pool(mypool)
Definition: pool_uuid.py:53
example
Definition: example.py:1
pool_uuid.printuuid
def printuuid(myuuid)
Definition: pool_uuid.py:63
pool_uuid.example
def example()
Definition: pool_uuid.py:89
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
pool_uuid.uuid_split
def uuid_split(guid)
Definition: pool_uuid.py:21
pool_uuid.pool_to_uuid
def pool_to_uuid(mypool)
Definition: pool_uuid.py:32
pool_uuid.usage
def usage()
Definition: pool_uuid.py:84
pool_uuid.timorder_uuid
def timorder_uuid(myuuid)
Definition: pool_uuid.py:42