/
snowflake
executable file
·44 lines (37 loc) · 2.18 KB
/
snowflake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
from __future__ import print_function # python2 compat
import argparse
import sys
import snowflake_uuid
parser = argparse.ArgumentParser(description="Generate IDs for a system that are as unique as a snowflake (they're just persistent UUIDs). Run with no arguments, simply prints the system's snowflake ID.",
epilog="The snowflake ID should be generated for the system when this package is installed. You can use this tool to generate an ID if for some reason one doesn't exist.")
parser.add_argument('-f', '--file', action='store', dest='sn_file', default=snowflake_uuid.SNOWFLAKE_FILE,
help="Use specified file to store id (default: %s)" % snowflake_uuid.SNOWFLAKE_FILE)
parser.add_argument('-m', '--make-snowflake', action='store_true', dest='make_snowflake',
help="Make a snowflake ID if one doesn't exist. If one already does, this does nothing, so it's always safe to use.")
parser.add_argument('--force-new-key', action='store_true', dest='force_new',
help="Force generation of new ID. WARNING: Deletes existing ID.")
args = parser.parse_args()
snowflake_file = args.sn_file
try:
if args.force_new:
print("WARNING! Forcing me to create new snowflake ID. Old snowflake ID was '%s'" % snowflake_uuid.snowflake(snowflake_file), file=sys.stderr)
snowflake_uuid._write_new_id(snowflake_file)
if args.make_snowflake:
snowflake_uuid.make_snowflake(snowflake_file)
id_ = snowflake_uuid.snowflake(snowflake_file)
if id_ == "":
print("Couldn't find a snowflake ID in '%s'. Try running with the -m option to make one." % snowflake_file, file=sys.stderr)
exit(1)
else:
print(id_)
exit(0)
except IOError as e:
if e.errno == 13: # permission denied
print("Permission denied when accessing '%s'. If you're making the snowflake for the first time for the system, try running this as root." % snowflake_file, file=sys.stderr)
exit(13)
elif e.errno == 2: # file not found
print("No snowflake file was found. Try generating one with 'snowflake -m'.", file=sys.stderr)
exit(2)
else:
raise e