Commit 91b102f0 authored by Sasa Zhang's avatar Sasa Zhang
Browse files

first perfect version of python3

parent bc784424
*.pyc
*.o
*.so
*.c
*.xml
*.json
*.xml.swp
*.memdump
*.txt
*.directory
cmd
ADD_TO_GENERIC_COMMAND_LINE
macadam.py
metro.log
.idea/
usr/share/metro/model/model
src/frontend/model
usr/share/metro/data/test_suite/test_suite_backup_on_Nov_4.py
usr/share/metro/data/test_suite/test_suite_error.py
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
#
#
# METRo : Model of the Environment and Temperature of Roads
# METRo is Free and is proudly provided by the Government of Canada
# Copyright (C) Her Majesty The Queen in Right of Canada, Environment Canada, 2006
#
# Questions or bugs report: metro@ec.gc.ca
# METRo repository: https://framagit.org/metroprojects/metro
# Documentation: https://framagit.org/metroprojects/metro/wikis/home
#
#
# Code contributed by:
# Miguel Tremblay - Canadian meteorological center
#
# $LastChangedDate$
# $LastChangedRevision$
#
########################################################################
###################################################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
......@@ -34,43 +31,38 @@
"""
Nom: check_translation
Nom: check_translation
Auteur: Miguel Tremblay
Auteur: Miguel Tremblay
Date: November 10th 2004
Description: Crer les fichiers .po correspondant au .py .
Ecrit ces fichiers dans /tmp/translate
Pour chacun de ces fichiers, il faut mettre la meme string qu'il y a dans
msgid a l'interieur de msgstr.
Compare ensuite les fichier dans /locale/en/LC_MESSAGES/*.po avec ceux
contenu dans /tmp/translate
S'il y a une difference, on fait un merge avec les fichiers dans en et fr.
Le fichier issue du merge porte le nom filename_merge.po.
On affiche la liste des fichiers qui ont ete modifies dans le fichier
merge.txt
Date: November 10th 2004
Description: Crer les fichiers .po correspondant au .py. Ecrit ces fichiers dans /tmp/translate pour chacun de
ces fichiers, il faut mettre la meme string qu'il y a dans msgid a l'interieur de msgstr.
Compare ensuite les fichier dans /locale/en/LC_MESSAGES/*.po avec ceux contenu dans /tmp/translate
s'il y a une difference, on fait un merge avec les fichiers dans en et fr.
Le fichier issue du merge porte le nom filename_merge.po.
On affiche la liste des fichiers qui ont ete modifies dans le fichier merge.txt
"""
import os
import os.path
import sys
import filecmp
from copy_msgid_msgstr import copy_msgid_msgstr
from scripts.copy_msgid_msgstr import copy_msgid_msgstr
sTemporaryDirectory = '/tmp/metro_translation/'
def main():
# Check if the .tar of the documentation is in argument.
if len(sys.argv) < 2:
sMessage = 'Usage: ' + sys.argv[0] + ' ../src/frontend/'
print sMessage
print(sMessage)
return
else:
check_translation(sys.argv[1])
def check_translation(sDirectory):
# Get a list of all the files in the directory
lAllFiles = []
......@@ -85,7 +77,7 @@ def check_translation(sDirectory):
nIndice = sFile.rfind('en/LC_MESSAGES/')
if nIndice < 0:
continue
sFilename = sFile[sFile.rfind('/')+1:len(sFile)-3]
sFilename = sFile[sFile.rfind('/') + 1:len(sFile) - 3]
if sExtension == 'po':
lPoFilepath.append(sFile)
lPoFilename.append(sFilename)
......@@ -94,17 +86,17 @@ def check_translation(sDirectory):
lPythonFilename = []
for sFile in lAllFiles:
sExtension = sFile[-2:]
sFilename = sFile[sFile.rfind('/')+1:len(sFile)-3]
sFilename = sFile[sFile.rfind('/') + 1:len(sFile) - 3]
if sExtension == 'py' and sFilename in lPoFilename:
lPythonFilepath.append(sFile)
# Create the temporary directory
if not os.path.exists(sTemporaryDirectory):
os.mkdir(sTemporaryDirectory)
# Create the .po files.
# Create the .po files.
for sFilepath in lPythonFilepath:
sFilename = sFilepath[sFilepath.rfind('/')+1:len(sFilepath)-3]
sPoFile = sTemporaryDirectory+sFilename+'.po'
sCommand = 'pygettext -o ' +sPoFile +' '+ sFilepath
sFilename = sFilepath[sFilepath.rfind('/') + 1:len(sFilepath) - 3]
sPoFile = sTemporaryDirectory + sFilename + '.po'
sCommand = 'pygettext -o ' + sPoFile + ' ' + sFilepath
# Create po file
os.system(sCommand)
# Copy directly msgid in msgstr
......@@ -114,65 +106,64 @@ def check_translation(sDirectory):
nIndice = sPoFilepath.rfind('/')
if nIndice < 0:
continue
sPoFilename = sPoFilepath[nIndice+1:len(sPoFilepath)-3]
sPoFilename = sPoFilepath[nIndice + 1:len(sPoFilepath) - 3]
if sFilename == sPoFilename:
sOriginalPoFilepath = sPoFilepath
break
# Compare the file with the famous command "msgcmp"
sCommand = 'msgcmp ' +sOriginalPoFilepath +' ' +sPoFile +\
" > /dev/null 2>&1"
sCommand = 'msgcmp ' + sOriginalPoFilepath + ' ' + sPoFile + " > /dev/null 2>&1"
# If the files are not compatible, the sys.command returns
# something that is not 0.
if os.system(sCommand):
# Create the merge file for english
sMergeFilenameEn = sFilename + '_merge_en.po'
sCommand = 'msgmerge ' +sOriginalPoFilepath +' ' +sPoFile\
+' -o ' +sMergeFilenameEn +\
sCommand = 'msgmerge ' + sOriginalPoFilepath + ' ' + sPoFile + ' -o ' + sMergeFilenameEn + \
" > /dev/null 2>&1"
os.system(sCommand)
# Get the file in french
sMergeFilenameFr = sFilename + '_merge_fr.po'
sPoFileFr = sPoFile.replace('/en/','/fr/')
sOriginalPoFilepathFr = sOriginalPoFilepath.\
replace('/en/','/fr/')
sCommand = 'msgmerge ' +sOriginalPoFilepathFr +' ' +\
sPoFileFr\
+' -o ' +sMergeFilenameFr +\
sMergeFilenameFr = sFilename + '_merge_fr.po'
sPoFileFr = sPoFile.replace('/en/', '/fr/')
sOriginalPoFilepathFr = sOriginalPoFilepath.replace('/en/', '/fr/')
sCommand = 'msgmerge ' + sOriginalPoFilepathFr + ' ' + sPoFileFr + ' -o ' + sMergeFilenameFr + \
" > /dev/null 2>&1"
os.system(sCommand)
# Add the file names in the list
lPoChanged.append(sOriginalPoFilepathFr)
lPoChanged.append(sOriginalPoFilepath)
print sMergeFilenameFr
print sMergeFilenameEn
print(sMergeFilenameFr)
print(sMergeFilenameEn)
# Write the file if there is any changed
if len(lPoChanged) > 0:
lPoChanged = map(lambda x:x+"\n", lPoChanged)
lPoChanged = [x + "\n" for x in lPoChanged]
file_with_filenames = open('changed_files', 'w')
file_with_filenames.writelines(lPoChanged)
####################################
# --------------------------------------------------------------------------------------------------------------------
# Name listAllFile_directory
#
# Parameters: list lFileNames : list of string to be returned with new string
# representing files.
# string sDir : directory in which an ls is performed.
# list lFiles : list of files to append in the directory.
#
#
# Author: Ben Park
# See post: http://groups.google.com/groups?hl=en&lr=&safe=off&selm=4cc96c48.0204031030.53bfb69f%40posting.google.com
#
####################################
#
# --------------------------------------------------------------------------------------------------------------------
def listAllFile_directory(lFileNames, sDir, lFiles):
def f1(a,sDir=sDir):
return os.path.join(sDir,a)
lFiles2 = map(f1, lFiles)
def f1(a, sDir=sDir):
return os.path.join(sDir, a)
lFiles2 = list(map(f1, lFiles))
lFileNames.extend(lFiles2)
if __name__ == "__main__":
main()
......
......@@ -4,19 +4,17 @@
# METRo : Model of the Environment and Temperature of Roads
# METRo is Free and is proudly provided by the Government of Canada
# Copyright (C) Her Majesty The Queen in Right of Canada, Environment Canada, 2006
#
# Questions or bugs report: metro@ec.gc.ca
# METRo repository: https://framagit.org/metroprojects/metro
# Documentation: https://framagit.org/metroprojects/metro/wikis/home
#
#
# Code contributed by:
# Miguel Tremblay - Canadian meteorological center
#
# $LastChangedDate$
# $LastChangedRevision$
#
########################################################################
###################################################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
......@@ -30,69 +28,51 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
"""
Nom: copy_msgid_msgstr
Nom: copy_msgid_msgstr
Auteur: Miguel Tremblay
Auteur: Miguel Tremblay
Date: November 10th 2004
Date: November 10th 2004
Description: Prend un fichier .po et copy le contenu de chaque msgid dans
le msgstr en bas de lui.
Description: Prend un fichier .po et copy le contenu de chaque msgid dans le msgstr en bas de lui.
"""
import os
import sys
def copy_msgid_msgstr(sFilename):
######## Open file ##############################
def copy_msgid_msgstr(sFilename):
# ------------------------------------------- Open file -------------------------------------------------------
file_original_po = open(sFilename)
sFile_ori_po = file_original_po.read()
file_original_po.close()
######## Split the file to msgid ##########################
# --------------------------------- Split the file to msgid -----------------------------------------------------
l_msgid = sFile_ori_po.split('msgid')
l_msgstr = sFile_ori_po.split('msgstr')
l_file = l_msgstr
# Display the msgid until msgstr is encontered
for i in range(1,len(l_msgid)):
# Display the msgid until msgstr is encountered
for i in range(1, len(l_msgid)):
sItem_msgid = l_msgid[i]
sItem_msgstr = l_msgstr[i]
# print sItem_msgid
# print sItem_msgstr
sReplace = sItem_msgid[:sItem_msgid.find('msgstr')]
# print sReplace
# Replace "" in msgstr by this string
sItem_msgstr = sItem_msgstr.replace('""', 'msgstr' + sReplace,1)
sItem_msgstr = sItem_msgstr.replace('""', 'msgstr' + sReplace, 1)
sItem_msgstr = sItem_msgstr[1:len(sItem_msgstr)]
# print "---"
# continue
l_file[i] = sItem_msgstr
# print "-----"
file_translated_po = open(sFilename, 'w')
file_translated_po.writelines(l_file)
return
def main():
if len(sys.argv) != 2:
print "Usage: copy_msgid_msgstr.py filename.po"
print("Usage: copy_msgid_msgstr.py filename.po")
return
copy_msgid_msgstr(sys.argv[1])
if __name__ == "__main__":
main()
if __name__ == "__main__":
main()
......@@ -4,20 +4,18 @@
# METRo : Model of the Environment and Temperature of Roads
# METRo is Free and is proudly provided by the Government of Canada
# Copyright (C) Her Majesty The Queen in Right of Canada, Environment Canada, 2006
#
# Questions or bugs report: metro@ec.gc.ca
# METRo repository: https://framagit.org/metroprojects/metro
# Documentation: https://framagit.org/metroprojects/metro/wikis/home
#
#
# Code contributed by:
# Miguel Tremblay - Canadian meteorological center
# Francois Fortin - Canadian meteorological center
#
# $LastChangedDate$
# $LastChangedRevision$
#
########################################################################
################################################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
......@@ -31,50 +29,43 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
"""
Nom: create_mo
Nom: create_mo
Auteur: Miguel Tremblay
Date: November 17th 2004
Auteur: Miguel Tremblay
Description: Crer les fichiers .mo correspondant au .po
dans un rpertoire.
Date: November 17th 2004
Description: Crer les fichiers .mo correspondant au .po dans un rpertoire.
"""
import sys
import os
def create_mo(sFile_with_filenames):
file_with_filenames = open(sFile_with_filenames)
def create_mo(sFile_with_filenames):
file_with_filenames = open(sFile_with_filenames)
lPoFiles = file_with_filenames.readlines()
for sPoFile in lPoFiles:
sMoFile = sPoFile[:-3]+'mo'
sCommand = 'msgfmt -o ' +sMoFile +' ' +sPoFile
print sCommand
sMoFile = sPoFile[:-3] + 'mo'
sCommand = 'msgfmt -o ' + sMoFile + ' ' + sPoFile
print(sCommand)
os.system(sCommand)
def main():
# Check if the .tar of the documentation is in argument.
if len(sys.argv) < 2:
sMessage = 'Usage: ' + sys.argv[0] + ' files.txt'
print sMessage
print(sMessage)
return
else:
create_mo(sys.argv[1])
create_mo(sys.argv[1])
if __name__ == "__main__":
main()
......@@ -3,20 +3,18 @@
#
# METRo : Model of the Environment and Temperature of Roads
# METRo is Free and is proudly provided by the Government of Canada
# Copyright (C) Her Majesty The Queen in Right of Canada, Environment Canada, 2006
# Copyright (C) Her Majesty The Queen in Right of Canada, Environment Canada, 2006
#
# Questions or bugs report: metro@ec.gc.ca
# METRo repository: https://framagit.org/metroprojects/metro
# Documentation: https://framagit.org/metroprojects/metro/wikis/home
#
#
# Code contributed by:
# Francois Fortin - Canadian meteorological center
#
# $LastChangedDate$
# $LastChangedRevision$
#
########################################################################
###################################################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
......@@ -31,20 +29,20 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
#
#
# Every file in the source directories is listed separatly so that we don't
# include unwanted files. This way, we are forced to consider what goes in the
# package and what doesn't each time we make modifications. We don't blindly
# Every file in the source directories is listed separatly so that we don't
# include unwanted files. This way, we are forced to consider what goes in the
# package and what doesn't each time we make modifications. We don't blindly
# add a directory with all the temporary and test files and it helps us to keep
# clean the distribution package.
#
import sys
import os
import re
sPackage_list = \
"""
"""
src/frontend/metro.py
src/frontend/metro_config.py
src/frontend/metro_logger.py
......@@ -144,42 +142,30 @@ README
LICENSE
INSTALL
setup.sh
"""
import string
import sys
import os
import re
import shutil
"""
VERSION_FILE = "../src/frontend/metro_config.py"
LINE_REGEX = 'CFG_METRO_VERSION\s*\=\s*[\"|\'][0-9]\.[0-9]\.[0-9][\"|\']'
VERSION_REGEX = '[0-9]\.[0-9]\.[0-9]'
def chomp(x):
while x != "" and x[-1] in "\r\n":
x = x[:-1]
return x
def extract_version_number( sFilename ):
def extract_version_number(sFilename):
sVersion_number = None
file = open(sFilename)
text = file.read()
file.close()
match_version_line =\
re.compile(LINE_REGEX)
match_version_line = re.compile(LINE_REGEX)
match_version_number = re.compile(VERSION_REGEX)
match_obj_version_line = match_version_line.search(text)
if match_obj_version_line:
start, stop = match_obj_version_line.span(0)
sVersion_line = text[start:stop]
......@@ -188,72 +174,62 @@ def extract_version_number( sFilename ):
start, stop = match_obj_version_number.span(0)
sVersion_number = sVersion_line[start:stop]
else:
print "Could not find the line containing the version number.\n" +\
"The regex is: '%s'." % LINE_REGEX
print("Could not find the line containing the version number.\n" + "The regex is: '%s'." % LINE_REGEX)
return sVersion_number
sVersion_number = extract_version_number(VERSION_FILE)
if not sVersion_number:
print "Could not find version number in file:'%s'. Aborting" % VERSION_FILE
print("Could not find version number in file:'%s'. Aborting" % VERSION_FILE)
sys.exit(1)
sMetro_dir = "metro-" + sVersion_number
# get path of metro parent directory
lPath = string.split(sys.path[0],"/")
lPath = sys.path[0].split("/")
sSvn_root_dir = lPath[-2]
sRoot_path = string.join(lPath[:-2],"/")
sMetro_real_dir = string.join(lPath[:-1],"/")
sPackage_path = string.join(lPath[:-2],"/")
sRoot_path = "/".join(lPath[:-2])
sMetro_real_dir = "/".join(lPath[:-1])
sPackage_path = "/".join(lPath[:-2])
# add leading metro directory name to each filename
sPackage_list = string.replace(sPackage_list,"\n","\n" + sSvn_root_dir + "/")
sPackage_list = sPackage_list.replace("\n", "\n" + sSvn_root_dir + "/")
# replace \n by a space character
sPackage_list = string.replace(sPackage_list,"\n"," ")
sPackage_list = sPackage_list.replace("\n", " ")
# remove any single sMetro_dir entry
sPackage_list = sPackage_list + " "
# do it 2 time (dirty hack)
sPackage_list = string.replace(sPackage_list," " + sSvn_root_dir + "/ "," ")
sPackage_list = string.replace(sPackage_list," " + sSvn_root_dir + "/ "," ")
sPackage_list = sPackage_list.replace(" " + sSvn_root_dir + "/ ", " ")
sPackage_list = sPackage_list.replace(" " + sSvn_root_dir + "/ ", " ")
# copy model binary to lib
#shutil.copy2( sRoot_path + "/" + sSvn_root_dir + "/usr/lib/metro/_macadam.so",
# sRoot_path + "/" + sSvn_root_dir + "/src/model/_macadam.so.prebuilt")
#shutil.copy2( sRoot_path + "/" + sSvn_root_dir + "/usr/share/metro/model/macadam.py",
# sRoot_path + "/" + sSvn_root_dir + "/src/model/macadam.py.prebuilt")
# tar command
sTarCommand = "tar cjvf " + sPackage_path + "/metro-" + sVersion_number + \
".tar.bz2 --exclude .svn --transform 's,^%s/,%s/,' " % (sSvn_root_dir, sMetro_dir) + " -C " + \
sRoot_path + " " + sPackage_list
#tar command
sTarCommand = "tar cjvf " + sPackage_path + "/metro-" + sVersion_number + \
".tar.bz2 --exclude .svn --transform 's,^%s/,%s/,' " % (sSvn_root_dir,sMetro_dir) + " -C " + \
sRoot_path + " " + sPackage_list
#execute tar command
print 'Executing', sTarCommand
# execute tar command
print('Executing', sTarCommand)
os.system(sTarCommand)
#sign command
# sign command
sSignCommand = "gpg --detach-sign " + sPackage_path + "/metro-" + sVersion_number + ".tar.bz2"
#sign package
print "\nMake a detached signature: '" + sSignCommand + "'"
# sign package
print("\nMake a detached signature: '" + sSignCommand + "'")
os.system(sSignCommand)
# cleanup
print "\n* Cleanup *"
print ""
print
print "* Package Creation Done *\n"
print "The METRo Package is located here:"
print "'" + sPackage_path + "/metro-" + sVersion_number + ".tar.bz2" + "'"
print ""
print "The METRo Package signature is located here:"
print "'" + sPackage_path + "/metro-" + sVersion_number + ".tar.bz2.sig" + "'"
print("\n* Cleanup *")
print("")
print()
print("* Package Creation Done *\n")
print("The METRo Package is located here:")
print("'" + sPackage_path + "/metro-" + sVersion_number + ".tar.bz2" + "'")
print("")
print("The METRo Package signature is located here:")
print("'" + sPackage_path + "/metro-" + sVersion_number + ".tar.bz2.sig" + "'")