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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#!/usr/local/bin/python

import glob
import filecmp
import hashlib
import os.path
import platform
import copy
import sys
import shutil
import operator
import json
import datetime
import math
import logging as log

from os.path import normpath
import os

import inspect

StartTime = datetime.datetime.now()
EndTime = datetime.datetime.now()

# --- Strings

def ToUpper(string):
    return string.upper()

def InsertStringInFilePath(file_path, string, file_extension):
    ext_len = len(file_extension) + 1
    file_path_p = file_path[:-ext_len] + '-' + string + file_path[-ext_len:]
    return file_path_p

# --- Strings

def LoadClassFromModule(module_name, class_name):

    __module = __import__(module_name)
    if __module:
        __class = getattr(__module, class_name)

        if __class:    
            return __class
        else:
            log.error("No class " + class_name + " found inside module " + module_name)
            return None
    else:
        log.error("No module " + module_name + " found")
        return None

def LoadFunctionFromModule(module_name, function_name):

    __module = __import__(module_name)
    if __module:
        __function = getattr(__module, function_name)

        if __function:    
            return __function
        else:
            log.error("No function " + function_name + " found inside module " + module_name)
            return None
    else:
        log.error("No module " + module_name + " found")
        return None

def Log2(x):
    return math.log(x, 2)

def FormatFloatNumber(num, i_format = '{:0>5d}', d_format = '{:.2f}' ):
    
    __str = i_format.format(int(num)) 
    __str = __str + d_format.format(num - int(num))[1:]

    return __str

def GetPlatform():
    return sys.platform

def GetOsName():
    return os.name

def GetPlatformSystem():
    return platform.system()

def IsOsLinux():
    return GetPlatformSystem() == "Linux"

def IsOsDos():
    return GetPlatformSystem() == "Windows"

def IsOsMac():
    return GetPlatformSystem() == "Darwin"

def GetNullFile():

    if IsOsDos():
        return "NUL"
    else:
        return "/dev/null"

def CompareFiles(file1, file2):
    return filecmp.cmp(file1, file2)

def AppendFile(file, text):    
    return open(file, "a").write(text + "\n")

def WriteFile(file, text):    
    return open(file, "w").write(text)

def ReadFile(file):    
    AssertFileExists(file)
    return open(file, "r").read()

def GenerateUniqueFileName(filename):

  from hashlib import md5
  from time import localtime

  return "%s_%s" % (md5(str(localtime())).hexdigest(), filename)

def FindInHash(key, hash, default):
    if key not in hash.keys():
        hash.update( { key : copy.deepcopy(default) } )

    return hash[key]

def GetJsonString(object):
    return json.dumps(object)

def WriteJsonFile(filepath, object):

    log.info("Writing JSON file " + filepath)

    json.dump(object, open(filepath, 'w'), indent = 4, )

def ReadJsonString(string):

    log.info("Reading JSON string: " + string)

    return json.loads(string)

def ReadJsonFile(filepath):

    log.info("Reading JSON file " + filepath)

    return ReadJsonString(ReadFile(filepath))

def LoadPythonFile(file_path):
    AssertFileExists(file_path)
    
    if globals is None:
        globals = {}
    globals.update({
        "__file__": file_path,
        "__name__": "__main__",
    })
    with open(file_path, 'rb') as file_path:
        exec(compile(file_path.read(), file_path, 'exec'), globals, locals)    


def Assert(condition, message = None):
    
    if condition == False:        
        if message == None:
            log.fatal("Assert Failed")
        else:
            log.fatal("Assert Failed: " + message)

def GetAbsoluteFilePaths( dir_path, file_names ):

    __file_path_list = []

    for __file_name in file_names:
        __file_path = dir_path + "/" + __file_name
        AssertFileExists(__file_path)

        __file_path_list.append(__file_path)

    return __file_path_list

def GetFileExt(x):
    x = os.path.splitext(x)[1] # Get extension

    return x[1:]

def GetFilePathWithoutExt(x):
    x = os.path.splitext(x)[0] # Remove extension
    return x

def GetFileBaseName(x):
    x = os.path.basename( x ) # Remove complete path and only get file name
    x = os.path.splitext(x)[0] # Remove extension
    return x

def GetDirBaseName(x):
    return os.path.basename( os.path.dirname(x) ) # Remove complete path and only get file name

def GetFileName(x):
    x = os.path.basename( x ) # Remove complete path and only get file name
    return x

def AssertFileExists(file, msg=None):
    if msg is None:
        msg = "File " + str(file) + " does not exist"
    Assert(FileExists(file), msg)

def AssertFileDoesNotExist(file):
    Assert(FileExists(file) == False, "File " + str(file) + " does not exist" )

def AssertDirExists(dir):
    Assert(DirExists(dir), "Directory " + str(dir) + " does not exist" )

def GetFileSize(file):
    return os.path.getsize(file)

def IsEmptyFile(file):
    if GetFileSize(file) == 0:
        return True
    else:
        return False

def RemoveFileIfEmpty(file):
    if IsEmptyFile(file):
        RemoveFile(file)

def RemoveFile(file):

    if FileExists(file):
        os.remove(file)

    log.info( 'RemoveFile ' + file )

def CopyFileSafe(old_file, new_file):
    AssertFileExists(old_file)

    if FileExists(new_file):
        log.fatal("File " + new_file + " already exists. Cannot overwrite it.")

    shutil.copy(old_file, new_file)

    log.info( 'CopyFileSafe ' + old_file + " to " + new_file )   
         
def CopyFile(old_file, new_file):
    AssertFileExists(old_file)

    shutil.copy(old_file, new_file)

    log.info( 'CopyFile ' + old_file + " to " + new_file )    

def MoveFile(old_file, new_file):
    AssertFileExists(old_file)

    log.info( 'MoveFile ' + old_file + " to " + new_file )    

    os.rename( old_file, new_file)

def MoveDir(old_dir, new_dir):
    AssertDirExists(old_dir)

    log.info( 'MoveDir ' + old_dir + " to " + new_dir )    

    os.rename( old_dir, new_dir)

def RemoveDir(dir):

    if os.path.isdir(dir) == True:
        log.info( 'RemoveDir ' + dir )
        shutil.rmtree(dir)

def GetExecutableFilePath(file_path):
    
    if IsOsDos():

        if GetFileExt( file_path ) != "exe":
            return file_path + ".exe"
        else:
            return file_path
    else:
        return file_path

def FileExists(file):
    if file is None or file == "":
        return False

    if os.path.isfile(file):
        return True
    else:
        return False

def DirExists(dir):
    
    if os.path.isdir(dir):
        return True
    else:
        return False

def GetRealPath(x):
    return os.path.realpath(x)

def GetFullPath(file_or_dir):
    if DirExists(file_or_dir):
        file_or_dir = os.path.realpath(file_or_dir)
        if file_or_dir[-1] == '/':
            return file_or_dir
        else:
            return file_or_dir + "/"

    elif FileExists(file_or_dir):
        return os.path.realpath(file_or_dir)

    return file_or_dir

def GetParentDir(dir):
    return GetFullPath( os.path.normpath(os.path.join(dir, "..")) )

def GetCurrentDir():
    return GetFullPath( os.getcwd() )

def ChangeDir(dir, quiet = False):
    
    os.chdir( dir )

    if quiet == False:
        log.info ('ChangeDir ' + dir )

def MakeFreshDir(dir):
    
    if os.path.isdir(dir) == True:
        RemoveDir(dir)

    MakeDir(dir)

def MakeFreshDirRecursive(path):
    
    RemoveDir(path)
    MakeDirRecursive(path)

def MakeDir(dir):
    
    if os.path.isdir(dir) == False:
        os.mkdir(dir)
        
        log.info ('MakeDir ' + dir )

def MakeDirRecursive(path):

    sub_path = os.path.dirname(path)

    if not DirExists(sub_path) and sub_path != "":
        MakeDirRecursive(sub_path)

    if not DirExists(path) and path != "":
        MakeDir(path)

def GetDirName(x):
    x = os.path.basename( normpath(x) ) # Remove complete path and only get file name
    return x

def GetFileListInDir(dir_path, file_ext, recursive = True):
    
    AssertDirExists(dir_path)

    __file_list = []

    if recursive:
        for __sub_dir_path, __dirs, __files in os.walk(dir_path):

            for __file_name in __files:

                __file_path = FormatFilePath( __sub_dir_path + '/' + __file_name )
                __file_ext  = GetFileExt( __file_path )

                if __file_ext.upper() == file_ext.upper():
                    __file_list.append( GetFullPath(__file_path) )
    else:
        __file_list = glob.glob(os.path.join( dir_path, '*.' + file_ext  ))

    return __file_list

def BuildFileList(file_or_folder_list, file_ext):
    
    __file_list = []

    for __file_or_folder in file_or_folder_list:

        if DirExists( __file_or_folder ):
            # Add all files in directory to the project
            __file_list_in_dir = GetFileListInDir( __file_or_folder, file_ext )

            for __file in __file_list_in_dir:
                __file_list.append( __file )

        elif GetFileExt(__file_or_folder).upper() == file_ext.upper():
            __file_list.append( GetFullPath(__file_or_folder) )

    return __file_list

# --- Other functions

def QuietExit(retcode = 0):
    sys.exit(retcode)

def Exit(retcode = 0):
    if retcode != 0:
        log.fatal("Exit with return code: " + str(retcode))
    else:
        QuietExit(0)

def Clip(x, min, max):
    if x > max:
        return max
    elif x < min:
        return min
    else:
        return x

def Sign(x):
    if x > 0:
        return 1
    elif x < 0:
        return -1
    else:
        return 0

def GetFileResolution(filepath):
    input_dir = os.path.dirname(os.path.realpath(filepath))
    input_dir_basename = os.path.basename( FormatDirPath( input_dir ) )

    file_basename = os.path.basename( filepath ) # Remove complete path and only get file name
    file_basename = os.path.splitext(file_basename)[0] # Remove extension
    
    itemslist = file_basename.split("_")

    # Try to get resolution from file name    
    if len(itemslist) > 1:
        resolution = itemslist[-1]

        if resolution.find('x') >= 0 :
            itemslist = resolution.split('x');            

            if len(itemslist) > 1:
                return int(itemslist[0]), int(itemslist[1])

    # Try to get resolution from parent folder name
    if input_dir_basename.find('x') >= 0 :
        itemslist = input_dir_basename.split('x');

        if len(itemslist) > 1:
            return int(itemslist[0]), int(itemslist[1])

    return 3000, 3000 # -- Default resolution

def WriteFolderMd5Sum(input_dir, file_ext, output_file, output_full_paths = False ):

    __md5sum_list = GetFolderMd5Sum( input_dir, file_ext )
    
    __md5_text = ""

    for __file_md5sum in __md5sum_list:

        __md5_text = __md5_text + __file_md5sum['md5sum']

        if output_full_paths:
            __md5_text = __md5_text + " " + __file_md5sum['file_path']
        else:
            __md5_text = __md5_text + " " + __file_md5sum['file_name']

        __md5_text = __md5_text + "\n"

    WriteFile( output_file, __md5_text )

    return __md5sum_list

def GetFolderMd5Sum(input_dir, file_ext ):

    __md5sum_list = []

    for __sub_dir_path, __dirs, __files in os.walk(input_dir):

        for __file_name in __files:

            __file_path = FormatFilePath( __sub_dir_path + '/' + __file_name )

            if GetFileExt(__file_path) == file_ext.lower() or GetFileExt(__file_path) == file_ext.upper():

                __md5sum_list.append( { 'file_path' : __file_path, 'file_name' : __file_name, 'md5sum' : GetFileMd5Sum(__file_path) } )

    return __md5sum_list

def GetFileMd5Sum(file_path, block_size = 256*128 ):
    md5 = hashlib.md5()

    with open(file_path,'rb') as f: 
        for chunk in iter(lambda: f.read(block_size), b''): 
             md5.update(chunk)

    return md5.hexdigest()

def FormatFilePath(file_path):
    file_path = file_path.rstrip('\\') # Remove \\ at the end
    file_path = file_path.rstrip('/')  # Remove / at the end
    
    file_path = file_path.replace('\\','/') # Replace backslash with forward slash

    # Convert relative path to absolute path
    file_path = os.path.abspath(file_path)

    file_path = file_path.replace('\\','/') # Replace backslash with forward slash

    # -- Make sure file_path exists before returning correct path
    AssertFileExists(file_path)

    return file_path

def GetAllSubDirs(dir_path):
    return [GetFullPath(dir_path + o) for o in os.listdir(dir_path) if os.path.isdir(os.path.join(dir_path,o))]

def FindAllFiles(dir_path, file_extension):    
    res = []
    for file_name in os.listdir(dir_path):
        file_path = GetFullPath(dir_path) + file_name
        if FileExists(file_path) and GetFileExt(file_path).upper() == file_extension.upper():
            res.append(file_path)
    return res
    
def FormatDirPath(dir_path, directory_must_exist = True):
    
    dir_path = dir_path.rstrip('\\') # Remove \\ at the end
    dir_path = dir_path.rstrip('/')  # Remove / at the end
    
    dir_path = dir_path.replace('\\','/') # Replace backslash with forward slash

    dir_path = os.path.abspath(dir_path)

    dir_path = dir_path.replace('\\','/') # Replace backslash with forward slash

    if directory_must_exist:
        # -- Make sure directory exists before returning correct path
        AssertDirExists(dir_path)

    return dir_path

def IsNumpyArray(x):
    return operator.isNumberType(x)

def IsInsidePackage():
    # Always assuming that root git dir is two levels over common directory
    git_dir = GetParentDir(__file__) + "/../../" + ".git"
    return DirExists(git_dir) == False

def CheckForCommands( command_list, verbsoe = True ):

    for __command in command_list:

        if GetFullPathOfCommand(__command) == None:
            
            if verbsoe:
                log.fatal("Command " + __command + " not found. Check if these apps are installed: " + str(command_list))

            return False

    return True

def GetFullPathOfCommand(command):

    __command = ""
    if IsOsDos():
        __command = "where.exe " + command
    else:
        __command = "which " + command

    __output = ExecuteCommand(__command, True)

    if __output["returncode"]:
        return None
    else:
        __path =  __output['stdout'].strip()
        
        log.info("Found " + command + ": " + __path)

        return FormatFilePath(__path)

def IsMyFunction(function_name):
    if (not function_name.startswith('_')) and inspect.isfunction(globals()[function_name]):
        return True
    else:
        return False

def GetFunctionArguments(function_name):

    if not inspect.getargspec(globals()[function_name]).args:
        return "()"
    else:
        __arg_info = inspect.getargspec(globals()[function_name])

        if __arg_info.defaults:

            __defaults_len = len(__arg_info.defaults)
            for __index in range(__defaults_len):
                __arg_list_index = len(__arg_info.args) - __index - 1
                __arg_info.args[__arg_list_index] = str(__arg_info.args[__arg_list_index]) + " = " + str(__arg_info.defaults[__defaults_len - __index - 1])

        return str(__arg_info.args)

def PrintFunctions(template = None):
    
    if template:
        print("Printing all available functions with word " + template)
    else:
        print("Printing all available functions")

    __count = 0
    for __function_name in globals().keys():
    
        if IsMyFunction(__function_name):

            if (template == None) or (template != None and __function_name.find(template) >= 0):            
                print(__function_name + GetFunctionArguments(__function_name))
                __count = __count + 1

    print(str(__count) + " Functions Found")

def ConvertStringArgumentToObject(argument):

    if argument == "None":
        return None

    if argument == "True":
        return True

    if argument == "False":
        return False

    try:
        return float(argument)
    except:
        pass

    try:        
        return int(argument)
    except:
        pass

    return argument

def GetModelDirPath(model_dir_path, source_dir_path):

    if DirExists(model_dir_path):
        return FormatDirPath(model_dir_path, True)

    model_dir_path = FormatDirPath(source_dir_path, False) + "/" + "models"
    if DirExists(model_dir_path):
        return FormatDirPath(model_dir_path, True)

    model_dir_path = os.environ.get('ROLL_MODELS_DIR')
    if model_dir_path is not None:
        if DirExists(model_dir_path):
            return FormatDirPath(model_dir_path, True)

    log.fatal("Could not find model directory - please make sure to specify model_dir_path")
    return ""

def GetFontsDirPath(fonts_dir_path, source_dir_path):

    if DirExists(fonts_dir_path):
        return FormatDirPath(fonts_dir_path, True)

    fonts_dir_path = FormatDirPath(source_dir_path, False) + "/" + "fonts"
    if DirExists(fonts_dir_path):
        return FormatDirPath(fonts_dir_path, True)

    fonts_dir_path = os.environ.get('ROLL_FONTS_DIR')
    if fonts_dir_path is not None:
        if DirExists(fonts_dir_path):
            return FormatDirPath(fonts_dir_path, True)

    log.fatal("Could not find fonts directory - please make sure to specify fonts_dir_path")
    return ""