-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathIrgFileFunctions.py
137 lines (107 loc) · 4.8 KB
/
IrgFileFunctions.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# __BEGIN_LICENSE__
# Copyright (c) 2009-2013, United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration. All
# rights reserved.
#
# The NGT platform is licensed under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# __END_LICENSE__
"""IrgFileFunctions.py - General functions for handling files"""
import sys, os, re, shutil, subprocess, string, time, errno
def createFolder(path):
"""Creates a folder if it does not already exist"""
if path == '':
return
if not os.path.exists(path):
os.mkdir(path)
def removeIfExists(path):
"""Removes a file if it exists"""
try:
os.remove(path)
except OSError as e:
if e.errno != errno.ENOENT: # Continue if the error is "no such file or directory"
raise # Re-raise the exception if a different error occured
def removeFolderIfExists(directory):
"""Removes a directory and everything in it"""
try:
shutil.rmtree(directory)
except OSError as e:
if e.errno != errno.ENOENT: # Continue if the error is "no such file or directory"
raise # Re-raise the exception if a different error occured
def replaceExtensionAndFolder(inputPath, outputFolder, newExtension):
"""Convenience function to replace the extension and the folder of a file path"""
newExt = os.path.splitext(inputPath)[0] + newExtension
return os.path.join(outputFolder, os.path.basename(newExt))
def fileIsNonZero(path):
'''Return true if the file exists and is non-empty'''
if os.path.isfile(path) and (os.path.getsize(path) > 0):
return True
else:
return False
def getFileLineCount(filePath):
"""Counts up the number of lines in a file"""
f = open(filePath)
i = 0
for line in f:
i = i + 1
return i
def checkIfToolExists(toolName):
"""Returns true if the system knows about the utility with this name (it is on the PATH)"""
# Look for the tool using the 'which' command
cmd = ['which', toolName]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
translateOut, err = p.communicate()
# Check if that command failed to find the file
failString = 'no ' + toolName + ' in ('
if translateOut.find(failString) >= 0:
raise Exception('Missing requested tool ' + toolName)
else:
return True
def getLastGitTag(codePath):
"""Returns the last brief git tag of the repository containing the file"""
# Get path to git folder
codeFolder = os.path.dirname(codePath)
gitFolder = os.path.join(codeFolder, '.git/')
# Get the tag using a subprocess call
cmd = ['git', '--git-dir', gitFolder, 'describe', '--abbrev=0']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
textOutput, err = p.communicate()
# Check for errors
if (textOutput.find('fatal:') >=0):
raise Exception('Error: getLastGitTag failed on code path ' + codePath)
return textOutput
def tarFileList(fileList, outputPath, compress=True, replacementNameList=None):
"""Creates a tar file containing a list of files with no absolute paths"""
# An extra set of commands is needed to strip the absolute path name from each stored file
tag = '-vcf'
if compress:
tag = '-jvcf'
cmd = 'tar ' + tag + ' ' + outputPath
if not replacementNameList: # Add all the files as they are
for f in fileList:
cmd = cmd + ' -C ' + os.path.dirname(f) + ' ' + os.path.basename(f) # Add the current path
else: # Use the replacement names for the files
for (f, r) in zip(fileList, replacementNameList):
os.rename(f, r) # Temporarily replace the file path
cmd = cmd + ' -C ' + os.path.dirname(r) + ' ' + os.path.basename(r) # Add the new path
# Command set up, now run it!
print cmd
os.system(cmd)
if replacementNameList: # Move all the files back to their original locations
for (f, r) in zip(fileList, replacementNameList):
os.rename(r, f)
def stripRgbImageAlphaChannel(inputPath, outputPath):
"""Makes an RGB copy of an RBGA image"""
cmd = 'gdal_translate ' + inputPath + ' ' + outputPath + ' -b 1 -b 2 -b 3 -co "COMPRESS=LZW" -co "TILED=YES" -co "BLOCKXSIZE=256" -co "BLOCKYSIZE=256"'
print cmd
os.system(cmd)