How to Create Installation Script for Tcl Package - installation

I have a Tcl package which consists of a couple modules, unit tests, and samples; which I am looking to distribute. I have searched, but not found any easy/simple way to create installation script for it. I looked at other packages such as Tclx and looks like they use autotools--an untouched territory for me. So, is autotools the only way? My target platforms are mostly Macs and Linux, but might expand to Windows in the future.

It seems I have to create my own setup script. Right now, it is very crude: copy files to a location. It works for me now. I will continue to enhance my setup script to fix bugs and add features as the need arise. Here is my setup.tcl
#!/usr/bin/tclsh
# Crude setup script for my package
# ======================================================================
# Configurable items
# ======================================================================
# Install dir: $destLib/$packagename
set destLib [file join ~ Library Tcl]
set packageName tcl_tools
# List of source/dest to install
set filesList {
argparser.tcl
dateutils.tcl
htmlutils.tcl
listutils.tcl
pkgIndex.tcl
samples/common.tcl
samples/dateutils_sample.tcl
samples/httputils_sample.tcl
samples/parse_by_example_demo_missing_parameter.tcl
samples/parse_by_example_demo.tcl
samples/parse_simple_demo.tcl
}
# ======================================================================
# Determine the destination lib dir
# ======================================================================
if {[llength $::argv] == 1} {
set destLib [lindex $::argv 0]
}
if {[lsearch $auto_path $destLib] == -1} {
puts "ERROR: Invalid directory to install. Must be one of these:"
puts "[join $auto_path \n]"
exit 1
}
set destLib [file join $destLib $packageName]
file mkdir $destLib
# ======================================================================
# Install
# ======================================================================
foreach source $filesList {
set dest [file join $destLib $source]
puts "Installing $dest"
# Create destination dir if needed
set destDir [file dirname $dest]
if {![file isdirectory $destDir]} { file mkdir $destDir }
# Copy
file copy $source $dest
}
puts "Done"

Related

Renaming two files in multiple folders by adding suffix and prefix

I have multiple folders where two files are present.
For example, 123.jpg, 456.jpg under folder ABC. I want to rename the files to IT1_ABC_123.v1.jpg and IT2_ABC_456.v1.jpg. Similarly, other folders have two files.
How can I do this in shell or Perl?
Try this, using shell and perl:
mkdir /tmp/test; cd $_
mkdir ABC DEF
touch {ABC,DEF}/{123,456}.jpg #creates four files, two in each directory
find|perl -nlE's,((.*)/(.+))/((123|456).jpg),$1/IT#{[++$n]}_$3_$4,&&say"$&\n$_\n"'
./ABC/123.jpg
./ABC/IT1_ABC_123.jpg
./ABC/456.jpg
./ABC/IT2_ABC_456.jpg
./DEF/123.jpg
./DEF/IT3_DEF_123.jpg
./DEF/456.jpg
./DEF/IT4_DEF_456.jpg
Now, after confirming this is what you want, replace the say with a rename:
find|perl -nlE's,((.*)/(.+))/((123|456).jpg),$1/IT#{[++$n]}_$3_$4, and rename$&,$_'
The new filenames:
find -type f
./ABC/IT1_ABC_123.jpg
./ABC/IT2_ABC_456.jpg
./DEF/IT3_DEF_123.jpg
./DEF/IT4_DEF_456.jpg
This will find filenames with 123.jpg or 456.jpg and rename them.
s,,, is the search-replace and it returns 1 (the number of changes it made) which again leads to the right side of the and being done (the rename).
Filenames that doesn't match 123.jpg or 456.jpg isn't renamed since s,,, will return 0 and the and is "short cutted" since it then logically cannot be true with a false (0) left side. So then the rename is not executed.
This variant does the same, but might be easier to read:
find|perl -nlE 'rename$&,$_ if s,((.*)/(.+))/((123|456).jpg),$1/IT#{[++$n]}_$3_$4,'
I have found this pattern useful in many cases of mass renamings. Also, dedicated software for mass renaming with GUIs exists, which for some might be easier to use.
Rewritten as a program abc.pl, it could be:
#!/usr/bin/perl
while(<>){
chomp;
next if not s,((.*)/([A-Z]{3}))/(\d{3}\.jpg),$1/IT#{[++$n]}_$3_$4,;
print "Found: $&\nNew name: $_\n\n";
#rename $&, $_;
}
Run:
find|perl abc.pl
You can do this in core Perl using the File::Find, File::Basename, and File::Copy modules. You can test it out with the script below. It won't make any changes until you uncomment the line with the "move" function.
#! perl
use strict;
use warnings;
use File::Basename;
use File::Copy;
use File::Find;
my $root_dir = '/path/to/main/folder';
# Recursively searches for all files below the $root_dir
my #fileset;
find(
sub {
# Get the absolute file path
my $path = $File::Find::name;
# Only capture the path if not a directory
# You can add any number of conditions here
if (!-d $path) {
push #fileset, $path;
}
},
$root_dir
);
# set the IT counter in new file name
my $int = 1;
# list of all possible file suffixes to have fileparse() look for. It will
# capture the end of the file path verbatim (including the period) if it's
# in this array
my #suffixes = ('.jpg', '.txt');
my $previous_dir;
foreach my $old_path (#fileset) {
# split apart the basename of the file, the directory path, and the file suffix
my ($basename, $parent_dir, $suffix) = fileparse($old_path, #suffixes);
# strip off trailing slash so fileparse() will capture parent dir name correctly
$parent_dir =~ s{[/]$}{};
# capture just the name of the parent directory
my $parent_name = fileparse($parent_dir);
# Assemble the new path
my $new_path = $parent_dir . '/IT' . $int . '_'
. $parent_name . '_' . "$basename.v1" . $suffix;
# Move the file to rename (this is safer than using rename() for cross-platform)
# move $old_path, $new_path;
print "OLD PATH: $old_path\n";
print "NEW PATH: $new_path\n\n";
# Reset counter when dir changes
if (!$previous_dir) {
$previous_dir = $parent_dir; # set previous_dir on first loop
}
elsif($previous_dir ne $parent_dir) {
$previous_dir = $parent_dir; # update previous_dir to check next loop
$int = 0; # reset counter
}
$int++; # iterate the counter
}
Edit 2018-07-12: I've updated the answer to show how to reset the counter when the directory changes by evaluating the current path with the one used in the previous loop and updating accordingly. This is not tested so it may need some adjustments.
Given the abc/def examples given, the output should look something like this:
OLD PATH: /path/to/main/folder/abc/123.jpg
NEW PATH: /path/to/main/folder/abc/IT1_abc_123.v1.jpg
OLD PATH: /path/to/main/folder/abc/456.txt
NEW PATH: /path/to/main/folder/abc/IT2_abc_456.v1.jpg
OLD PATH: /path/to/main/folder/def/123.jpg
NEW PATH: /path/to/main/folder/def/IT1_def_123.v1.jpg
OLD PATH: /path/to/main/folder/def/456.jpg
NEW PATH: /path/to/main/folder/def/IT2_def_456.v1.jpg

In rake, how to create a package that contains a file but renames it inside the package

I would like to create a package that contains a file but renames it inside the package.
For example:
Rake::PackageTask.new("rake", "1.2.3") do |p|
p.package_files.include("aa.rb")
end
I would like aa.rb to be named bb.rb inside the package.
How can I do this elegantly?
Looking at the PackageTask source, it seems you could define a new task (say rename_files) that depends on on the p.package_dir_path task defined by Rake::PackageTask. In rename_files task you can rename the file link(s) which package_dir_path task made in package_dir. Then you add your new rename_files task as a dependency for each of the "#{package_dir}/#{[tar|zip|etc]_file}" task targets you care about.
With these dependencies, the order of operations should become:
set up package_dir with links to source files from package_files
rename links with your injected dependency
execute archive creation command on package_dir
If this isn't clear enough to get you there, I'll try and post some actual code later.
[LATER] Ok, some code. I made a sample project which looks like this:
$ find .
.
./lib
./lib/aa.rb
./lib/foo.rb
./Rakefile
And in the Rakefile, I define a package task as:
require 'rake/packagetask'
Rake::PackageTask.new('test', '1.2.3') do |p|
p.need_tar = true
p.package_files.include('lib/**/*')
task :rename_files => [ p.package_dir_path ] do
fn = File.join( p.package_dir_path, 'lib', 'aa.rb' )
fn_new = File.join( p.package_dir_path, 'lib', 'bb.rb' )
File.rename( fn, fn_new )
end
[
[p.need_tar, p.tgz_file, "z"],
[p.need_tar_gz, p.tar_gz_file, "z"],
[p.need_tar_bz2, p.tar_bz2_file, "j"],
[p.need_zip, p.zip_file, ""]
].each do |(need, file, flag)|
task "#{p.package_dir}/#{file}" => [ :rename_files ]
end
end
The logic here is what I explained above. Running it, you can see that the hard link made in the package dir is renamed from "aa.rb" to "bb.rb", then we tar the directory and viola!
$ rake package
(in /Users/dbenhur/p)
mkdir -p pkg
mkdir -p pkg/test-1.2.3/lib
rm -f pkg/test-1.2.3/lib/aa.rb
ln lib/aa.rb pkg/test-1.2.3/lib/aa.rb
rm -f pkg/test-1.2.3/lib/foo.rb
ln lib/foo.rb pkg/test-1.2.3/lib/foo.rb
cd pkg
tar zcvf test-1.2.3.tgz test-1.2.3
a test-1.2.3
a test-1.2.3/lib
a test-1.2.3/lib/bb.rb
a test-1.2.3/lib/foo.rb
cd -
Here's the tar manifest with "bb.rb" instead of "aa.rb":
$ tar tf pkg/test-1.2.3.tgz
test-1.2.3/
test-1.2.3/lib/
test-1.2.3/lib/bb.rb
test-1.2.3/lib/foo.rb

SVN pre-commit hook to ensure all files referenced in .csproj are versioned?

Occasionally we commit a C# project file to SVN that references files we forgot to add to SVN. Are there any pre-commit hook scripts out there that parse the .csproj file and reject the commit if it references unversioned files?
#!/usr/bin/perl
# Checks for source files which have been added to a csproj in a commit
# but haven't themselves been committed.
use Modern::Perl;
use warnings FATAL => 'syntax';
use File::Basename qw(basename);
use XML::Simple;
die "usage: $0 repo transaction\n" if #ARGV != 2;
my $opt = "-t"; # -r for testing
my ($repos, $txn) = #ARGV;
# If you really, really want to add a file to the proj and
# not commit it, start your commit message with a !
my #info = `svnlook info $opt $txn "$repos"`;
exit 0 if ($info[3] =~ /\A!/);
my #lines = `svnlook changed $opt $txn "$repos"`;
my #projects = grep { /\AU/ }
grep { /[.]csproj\z/ }
map { chomp; $_ } #lines;
my #filelist = `svnlook tree $opt $txn "$repos" --full-paths`;
my %present;
foreach (#filelist) {
chomp;
$present{$_} = 1;
}
foreach (#projects) {
m"\AU.\s\s([\w/.]+/)([\w]+\.csproj)\z" or die "bad line $_";
my ($path, $proj) = ($1, $2);
my $projfile = `svnlook cat $opt $txn "$repos" $path/$proj`;
my $xml = XMLin($projfile);
# Tested with VS 2012 project files
my #includes = #{$xml->{ItemGroup}->[1]->{Compile}};
# All the source files in the csproj
my #filenames = map {$_->{Include}} #includes;
foreach (#filenames) {
# ignore "../etc", not below the project file in the tree
next if /\A[.][.]/;
# if you have files that are in the proj but shouldn't be committed
# eg some generated files, add checks for them here
# next if /MyGeneratedFile.cs\z/;
my $file = $path . $_;
# The csproj file speaks windows paths, but svn will output unix ones
$file =~ tr|\\|/|;
if (!defined $present{$file}) {
die "The file $file is included in the project $path\\$proj, but is not present in the tree, did you forget to commit it?";
}
}
}
If you are using a windows server, you can have a look at Subversion Notify for Windows -http://sourceforge.net/projects/svn-notify/
I use it to do a simple check on the commit message, to ensure users confirm with our in house rules. I have not gone into any of the other pre-commit uses, but it might be worth a shot!
I Quote from the Manual:
PRE-COMMIT CHECKS
 Ensure the integrity of your repository by enforcing commit message standards
 Restrict the types of files that can be committed (eliminate accidental commits of temporary or developer specific configuration files)
 Enforce file type checks - files that can be committed, but require a special commit tag to make sure it's being committed as per your rules
 Check against your task tracking/ bug tracking system to ensure it's a valid tracking item and your standards are enforced (no commits against closed bugs for example)

How to add/use a variable to my bashrc file?

I'm a newbie to Linux operating system
I need to do the following:-
I have multiple projects under "~/myprojects"
Think of like >ls ~/myprojects
project1 project2i newproject project_possible....
All my projects have a fixed structure see as below:-
ls ~/myprojects/
src lib inc common test_scripts
(all these are directories having some files in them
For navigating the current()
I want to do something like this in my bashrc file.
assign curr_project = "$1"
alias psrc='cd ~/myprojects/curr_project/src/'
alias plib='cd ~/myprojects/curr_project/lib/'
Thanks in advance
You can use an environment variable to specify the current project and use the variable in your aliases:
current() {
export CURR_PROJECT=$1
}
alias psrc='cd ~/myprojects/$CURR_PROJECT/src/'
alias plib='cd ~/myprojects/$CURR_PROJECT/lib/'
First you set the CURR_PROJECT by using
$ current project1
Then you call your alias to change directories:
$ psrc
Hope that helps.
I use something similar for my work environment - many projects with a common directory structures. I also use a selector to allow me choose projects quickly without typing their name. You may find it useful.
E.g.
current()
{
export PROJECT_ROOT=~/myprojects
# If you pass a project name, use it, otherwise print a list
# for the user to select
if [ -n "$1" ]; then
export CURRENT_PROJECT=$1
else
# Find subdirectories in PROJECT_ROOT
SUBDIRS=`find $PROJECT_ROOT -mindepth 1 -maxdepth 1 -type d -printf "%f "`
if [ -n "$SUBDIRS" ]; then
PS3="Select project: "
select d in $SUBDIRS; do
if [[ -n $d ]]; then
export CURRENT_PROJECT=$d
break
else
echo "Bad choice"
return
fi
done
else
echo "No projects found"
return
fi
fi
# Now we have the CURRENT_PROJECT name, set up the aliases
alias psrc='cd $PROJECT_ROOT/$CURRENT_PROJECT/src/'
alias plib='cd $PROJECT_ROOT/$CURRENT_PROJECT/lib/'
}
Then if you type "current", you will get a choice:
~$ current
1) proj1
2) proj2
3) proj3
Select project:
This is a real time-saver for me - maybe it will be for you too.

How do you find the paths of linked images in Adobe Illustrator 9? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have an Illustrator file with linked images. I'd actually like to instead embed the images. I first have to know which files they are. How do I find out? I'm using Illustrator 9.
The first Illustrator version I ever used was 10, but, is there a links pallete in version 9? Try the window menu and look for "links". It has a few options there to search for the image you want, relink, open the original, etc.
Great to know the poster is set, but this doesn't answer the question. In CS3, if you double-click on the image in links palette, ti will bring up Link Info for the linked element, which shows the path to the file (provided it isn't longer than the window).
Maybe the older versions allow you to do this too.
Unfortunately, if you're dealing with a missing link element (which you ignored to fix upon opening the file), this field is blank. Illustrator sucks in comparison to InDesign for file packaging and linking. It would be nice if it could package files like InDesign, and store relative references to the external resources.
With newer versions of Illustrator you might be able to replace links to broken images using this script:
http://techblog.willshouse.com/2011/01/16/update-illustrator-linked-files-script/
I just had this problem in Illustrator CS4; a lot of my stuff was archived recently.
Click on the "missing" image in the artboard.
In the top left you will see the file name displayed.
Click "edit original" in the subsequent drop down menu. Illustrator will attempt to find the file, and flash a warning window "windows cannot find file" etc giving you the full file location.
This is useful as edit original is greyed out in the links window. And very useful for people like me who have a vast library of files.
I use the following perl script to keep track of linked images in Illustrator files. This is especially helpful for broken links, because it will still tell you the full path to the linked image by peeking inside the Illustrator file. It obviously does more than anyone here needs, but perhaps it will be useful. The help should explain how to use it. On my machine I have called it ailinkedfiles.pl and I have put it in ~/bin which is in my PATH.
#!/usr/bin/perl
# program to find the linked files inside an Adobe Illustrator file
require 5.004;
use File::Basename; # to extract a filename from a full path
use File::Find; # to build a list of files
use File::Spec; # Platform independent way to build paths
use vars qw/ %opt /; # for command line options - see init()
use strict;
init(); # process command line options
# Autoflush stdout
$|=1;
if ($opt{p}){
die "Did you really mean to call your script ".$opt{p}."!\n" if($opt{p} =~ /\.ai$/i);
print "Generating script file $opt{p}\n" if $opt{v};
open SCRIPT, "> $opt{p}";
}
die "No input specified; use ".basename($0)." -h for help\n" if(#ARGV==0);
my $arg; foreach $arg (#ARGV){
if(-d $arg){
# nb it is necesary to convert the directory specification
# to an absolute path to ensure that the open in &findLinkedFiles
# works properly during multi directory traversal
my $InDir=File::Spec->rel2abs($arg);
find(\&handleFind,$InDir);
} elsif (-f $arg) {
my $InDir=File::Spec->rel2abs(dirname($ARGV[0]));
&findLinkedFiles(File::Spec->rel2abs($ARGV[0]),$InDir) ;
# &findLinkedFiles(File::Spec->rel2abs($arg)) ;
}
}
sub init()
# copied from: http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/getopts
{
use Getopt::Std; # to handle command line options
my $opt_string = 'hvlzdsftnp:ux:';
getopts( "$opt_string", \%opt ) or usage();
usage() if $opt{h};
}
# Print out usage information
sub usage()
{
print STDERR << "EOF";
Usage: $0 [OPTIONS] <AIFILE/DIR>
Parse an Adobe Illustrator file or (recursively) parse a directory of ai files
and print a list of the linked files to STDOUT. These could be piped to xargs eg:
$0 aifile.ai | xargs -I {} ln -vs
-h print this help
-v verbose ouput
-s print file names with short path
-d print current directory on each line
-n no-print (suppresses printing of linked file names)
-x <regex> exclude files whose full path matches regex
-l symlink in current directory if file linked from Illustrator file exists somewhere else
-f force symlink to overwrite existing target file
-t test run
-p <file> write commands to a script file
-u status of src and target
- doesn't exist
F plain file
L symbolic link
E exists (unknown file type)
Note that src is the link contained in the Illustrator file and
target is a file of the same name in the same directory as the Illustrator file
If the status is -- you will have problems in Illustrator
If the status is -F Illustrator will substitute the local file for the unavailable linked file
If the status is F- you can run this script with the -s option to make a symlink
If the status is FF then Illustrator will be happy
EOF
exit();
}
sub mysymlink{
my ($src,$targetdir)=#_;
my $target=File::Spec->catdir($targetdir,basename($src));
if(File::Spec->rel2abs($src) eq File::Spec->rel2abs($target)){
print "src and target identical for src=$src\n" if $opt{v};
return;
}
if(-e $src){
my $opts=$opt{f}?"-fsv":"-sv";
my $cmd="ln $opts \"$src\" \"$target\"";
myexec("$cmd");
} else {
print "No link made: $src doesn't exist\n" if $opt{v};
}
}
sub myexec {
my ($cmd) = #_;
if ($opt{t}){
print STDERR "test: $cmd\n";
} elsif ($opt{p}){
print SCRIPT $cmd,"\n";
} else {
# should get to see output with system
print STDERR "run: $cmd\n" if $opt{v};
return system $cmd;
}
}
sub mystatus{
my ($src,$targetdir)=#_;
my $target=File::Spec->catdir($targetdir,basename($src));
my ($ss,$ts)=("-","-");
$ss = "E" if(-e $src);
$ss = "F" if(-f $src);
$ss = "L" if(-l $src);
$ts = "E" if(-e $target);
$ts = "F" if(-f $target);
$ts = "L" if(-l $target);
return ($ss.$ts);
}
# This extracts the file info from the header
sub handleFind{
# get the file name
my $FullFoundFile = $File::Find::name;
#print $FullFoundFile,"\n";
return if ($opt{x} and $FullFoundFile =~ /$opt{x}/i);
# parse if it ends in ai
findLinkedFiles($FullFoundFile, $File::Find::dir) if ($FullFoundFile =~ /\.ai$/i);
}
# This does the actual parsing of the Illustrator Files
sub findLinkedFiles{
my ($InFile,$InDir)=#_;
# protect with escaped quotes for shell if non-empty
my $ProtectedInDir=$InDir?"\"$InDir\"":$InDir;
die "Can't open $InFile \: $!\n" unless open(AIFILE, "<$InFile");
binmode(AIFILE);
# %%DocumentFiles is the starting point
$/="%%";
my #lines = readline<AIFILE>;
if(#lines==0){
print STDERR "can't read header of $InFile\n" if $opt{v} ; # the header length
return;
}
print "################\n";
if ($opt{s}){
print "# FILE = ",basename($InFile),"\n";
} else {
print "# FILE = ",$InFile,"\n";
}
for my $i ( 0 .. $#lines ){
# if ( $lines[$i]=~/^DocumentFiles\:(.*?)\W+%%/){
# not sure why we need two % signs here
if ( $lines[$i]=~/^DocumentFiles\:(.*?)\W+%/){
print mystatus($1,$InDir)," " if $opt{u} and not $opt{n};
print "\"$1\" ",$opt{d}?$ProtectedInDir:"","\n" unless $opt{n};
$i++;
mysymlink($1,$InDir) if $opt{l};
while($lines[$i]=~/^[+](.*?)\W\%.*$/){
# print "\"$1\" $InDir\n"; $i++;
print mystatus($1,$InDir)," " if $opt{u} and not $opt{n};
print "\"$1\" ",$opt{d}?$ProtectedInDir:"","\n"unless $opt{n};
$i++;
mysymlink($1,$InDir) if $opt{l};
}
}
}
}

Resources