أداة لإضافة رؤوس الترخيص إلى الملفات المصدر؟[مغلق]
-
02-07-2019 - |
سؤال
أنا أبحث عن أداة من شأنها أن تضيف، بشكل مجمّع، رأس ترخيص إلى بعض الملفات المصدر، وبعضها يحتوي بالفعل على الرأس.هل هناك أداة يمكنها إدراج رأس، إذا لم تكن موجودة بالفعل؟
يحرر:أنا لا أضع علامة على إجابة هذا السؤال عمدًا، نظرًا لأن الإجابات كلها خاصة بالبيئة وذاتية
المحلول
#!/bin/bash
for i in *.cc # or whatever other pattern...
do
if ! grep -q Copyright $i
then
cat copyright.txt $i >$i.new && mv $i.new $i
fi
done
نصائح أخرى
حل بايثون، تعديل لاحتياجاتك الخاصة
سمات:
- يتعامل مع رؤوس UTF (مهم لمعظم IDEs)
- يقوم بتحديث جميع الملفات بشكل متكرر في الدليل الهدف من خلال تمرير القناع المحدد (تعديل المعلمة .endswith لقناع الملف الخاص بلغتك (.c، .java، ..etc)
- القدرة على الكتابة فوق نص حقوق الطبع والنشر السابق (قدم معلمة حقوق الطبع والنشر القديمة للقيام بذلك)
- يحذف بشكل اختياري الدلائل الواردة في مصفوفة Exceptdir
-
# updates the copyright information for all .cs files
# usage: call recursive_traversal, with the following parameters
# parent directory, old copyright text content, new copyright text content
import os
excludedir = ["..\\Lib"]
def update_source(filename, oldcopyright, copyright):
utfstr = chr(0xef)+chr(0xbb)+chr(0xbf)
fdata = file(filename,"r+").read()
isUTF = False
if (fdata.startswith(utfstr)):
isUTF = True
fdata = fdata[3:]
if (oldcopyright != None):
if (fdata.startswith(oldcopyright)):
fdata = fdata[len(oldcopyright):]
if not (fdata.startswith(copyright)):
print "updating "+filename
fdata = copyright + fdata
if (isUTF):
file(filename,"w").write(utfstr+fdata)
else:
file(filename,"w").write(fdata)
def recursive_traversal(dir, oldcopyright, copyright):
global excludedir
fns = os.listdir(dir)
print "listing "+dir
for fn in fns:
fullfn = os.path.join(dir,fn)
if (fullfn in excludedir):
continue
if (os.path.isdir(fullfn)):
recursive_traversal(fullfn, oldcopyright, copyright)
else:
if (fullfn.endswith(".cs")):
update_source(fullfn, oldcopyright, copyright)
oldcright = file("oldcr.txt","r+").read()
cright = file("copyrightText.txt","r+").read()
recursive_traversal("..", oldcright, cright)
exit()
تفحص ال حقوق الطبع والنشر-رأس روبيجيم.وهو يدعم الملفات ذات الامتدادات التي تنتهي بـ php، c، h، cpp، hpp، hh، rb، css، js، html.ويمكنه أيضًا إضافة وإزالة الرؤوس.
قم بتثبيته عن طريق كتابة "sudo gem install copyright-header
"
بعد ذلك، يمكن أن تفعل شيئا مثل:
copyright-header --license GPL3 \
--add-path lib/ \
--copyright-holder 'Dude1 <dude1@host.com>' \
--copyright-holder 'Dude2 <dude2@host.com>' \
--copyright-software 'Super Duper' \
--copyright-software-description "A program that makes life easier" \
--copyright-year 2012 \
--copyright-year 2012 \
--word-wrap 80 --output-dir ./
كما أنه يدعم ملفات الترخيص المخصصة باستخدام الوسيطة --license-file.
إليك نص Bash الذي سيفي بالغرض، بافتراض أن لديك رأس الترخيص في الملف License.txt:
ملف addlicense.sh:
#!/bin/bash
for x in $*; do
head -$LICENSELEN $x | diff license.txt - || ( ( cat license.txt; echo; cat $x) > /tmp/file;
mv /tmp/file $x )
done
الآن قم بتشغيل هذا في الدليل المصدر الخاص بك:
export LICENSELEN=`wc -l license.txt | cut -f1 -d ' '`
find . -type f \(-name \*.cpp -o -name \*.h \) -print0 | xargs -0 ./addlicense.sh
يحرر:إذا كنت تستخدم Eclipse، فهناك البرنامج المساعد
لقد كتبت نصًا بسيطًا بيثون بناءً على رد Silver Dragon.كنت بحاجة إلى حل أكثر مرونة لذلك توصلت إلى هذا.يسمح لك بإضافة ملف رأس إلى كافة الملفات الموجودة في الدليل، بشكل متكرر.يمكنك اختياريًا إضافة تعبير عادي يجب أن تتطابق معه أسماء الملفات، وتعبير عادي يجب أن تتطابق مع أسماء الدليل وتعبير عادي لا يجب أن يتطابق مع السطر الأول في الملف.يمكنك استخدام هذه الوسيطة الأخيرة للتحقق مما إذا كان الرأس مدرجًا بالفعل.
سيتخطى هذا البرنامج النصي تلقائيًا السطر الأول في الملف إذا كان يبدأ بـ shebang (#!).هذا حتى لا يكسر البرامج النصية الأخرى التي تعتمد على هذا.إذا كنت لا ترغب في هذا السلوك، فسيتعين عليك التعليق بثلاثة أسطر في رأس الكتابة.
ها هو:
#!/usr/bin/python
"""
This script attempts to add a header to each file in the given directory
The header will be put the line after a Shebang (#!) if present.
If a line starting with a regular expression 'skip' is present as first line or after the shebang it will ignore that file.
If filename is given only files matchign the filename regex will be considered for adding the license to,
by default this is '*'
usage: python addheader.py headerfile directory [filenameregex [dirregex [skip regex]]]
easy example: add header to all files in this directory:
python addheader.py licenseheader.txt .
harder example adding someone as copyrightholder to all python files in a source directory,exept directories named 'includes' where he isn't added yet:
python addheader.py licenseheader.txt src/ ".*\.py" "^((?!includes).)*$" "#Copyright .* Jens Timmerman*"
where licenseheader.txt contains '#Copyright 2012 Jens Timmerman'
"""
import os
import re
import sys
def writeheader(filename,header,skip=None):
"""
write a header to filename,
skip files where first line after optional shebang matches the skip regex
filename should be the name of the file to write to
header should be a list of strings
skip should be a regex
"""
f = open(filename,"r")
inpt =f.readlines()
f.close()
output = []
#comment out the next 3 lines if you don't wish to preserve shebangs
if len(inpt) > 0 and inpt[0].startswith("#!"):
output.append(inpt[0])
inpt = inpt[1:]
if skip and skip.match(inpt[0]): #skip matches, so skip this file
return
output.extend(header) #add the header
for line in inpt:
output.append(line)
try:
f = open(filename,'w')
f.writelines(output)
f.close()
print "added header to %s" %filename
except IOError,err:
print "something went wrong trying to add header to %s: %s" % (filename,err)
def addheader(directory,header,skipreg,filenamereg,dirregex):
"""
recursively adds a header to all files in a dir
arguments: see module docstring
"""
listing = os.listdir(directory)
print "listing: %s " %listing
#for each file/dir in this dir
for i in listing:
#get the full name, this way subsubdirs with the same name don't get ignored
fullfn = os.path.join(directory,i)
if os.path.isdir(fullfn): #if dir, recursively go in
if (dirregex.match(fullfn)):
print "going into %s" % fullfn
addheader(fullfn, header,skipreg,filenamereg,dirregex)
else:
if (filenamereg.match(fullfn)): #if file matches file regex, write the header
writeheader(fullfn, header,skipreg)
def main(arguments=sys.argv):
"""
main function: parses arguments and calls addheader
"""
##argument parsing
if len(arguments) > 6 or len(arguments) < 3:
sys.stderr.write("Usage: %s headerfile directory [filenameregex [dirregex [skip regex]]]\n" \
"Hint: '.*' is a catch all regex\nHint:'^((?!regexp).)*$' negates a regex\n"%sys.argv[0])
sys.exit(1)
skipreg = None
fileregex = ".*"
dirregex = ".*"
if len(arguments) > 5:
skipreg = re.compile(arguments[5])
if len(arguments) > 3:
fileregex = arguments[3]
if len(arguments) > 4:
dirregex = arguments[4]
#compile regex
fileregex = re.compile(fileregex)
dirregex = re.compile(dirregex)
#read in the headerfile just once
headerfile = open(arguments[1])
header = headerfile.readlines()
headerfile.close()
addheader(arguments[2],header,skipreg,fileregex,dirregex)
#call the main method
main()
بالنسبة لجافا، http://code.google.com/p/maven-license-plugin/
أطيب التحيات
حسنًا، إليك أداة واجهة مستخدم بسيطة تعمل بنظام التشغيل Windows فقط، حيث تبحث عن جميع الملفات من النوع المحدد في مجلد، وتضيف النص الذي تريده إلى الأعلى (نص الترخيص الخاص بك)، وتنسخ النتيجة إلى دليل آخر (تجنب مشاكل الكتابة الفوقية المحتملة) .إنه مجاني أيضًا.مطلوب .نت 4.0.
أنا المؤلف فعليًا، لذا لا تتردد في طلب إصلاحات أو ميزات جديدة...لا توجد وعود بشأن جدول التسليم بالرغم من ذلك.;)
مزيد من المعلومات: أداة رأس الترخيص في أمازيفي.كوم
تحقق من مُضيف الترخيص.وهو يدعم ملفات التعليمات البرمجية المتعددة (حتى تلك المخصصة) ويتعامل مع الرؤوس الموجودة بشكل صحيح.يأتي بالفعل مزودًا بنماذج لتراخيص المصادر المفتوحة الأكثر شيوعًا.
هنا واحد لقد وجدت في قائمة أباتشي.إنه مكتوب بلغة روبي ويبدو أنه من السهل قراءته.يجب أن تكون قادرًا على تسميتها من Rake للحصول على المزيد من اللطف الخاص.:)
إليك واحدة قمت بإدخالها في PHP لتعديل ملفات PHP.كان لدي أيضًا معلومات ترخيص قديمة أريد حذفها، لذا يتم استبدال النص القديم أولاً، ثم يضيف النص الجديد فورًا بعد الفتح
<?php
class Licenses
{
protected $paths = array();
protected $oldTxt = '/**
* Old license to delete
*/';
protected $newTxt = '/**
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/';
function licensesForDir($path)
{
foreach(glob($path.'/*') as $eachPath)
{
if(is_dir($eachPath))
{
$this->licensesForDir($eachPath);
}
if(preg_match('#\.php#',$eachPath))
{
$this->paths[] = $eachPath;
}
}
}
function exec()
{
$this->licensesForDir('.');
foreach($this->paths as $path)
{
$this->handleFile($path);
}
}
function handleFile($path)
{
$source = file_get_contents($path);
$source = str_replace($this->oldTxt, '', $source);
$source = preg_replace('#\<\?php#',"<?php\n".$this->newTxt,$source,1);
file_put_contents($path,$source);
echo $path."\n";
}
}
$licenses = new Licenses;
$licenses->exec();
إذا كنت لا تزال بحاجة إلى واحدة، فهناك أداة صغيرة قمت بكتابتها، وأسميتها سركهيد.يمكنك العثور عليه في http://www.solvasoft.nl/downloads.html
إذا كنت تستخدم sbt، هناك https://github.com/Banno/sbt-license-plugin