Why Python Rocks Your Mass File Operations

I've been fooling around with Python for mass file operations for a while now.  It's been good so far, especially with how friggin' simple it is to do just about ANY file operation. 

At work, I sometimes get requested to run mass site updates.  Commonly, I'll be asked to do the same thing to over 30 sites.  In these instances, I've found that writing a Python script to handle at least some of that lifting is worthwhile.

In today's example, we're going to cover updating the source files for a bunch of sites.  These are dirty hacks, and Windows specific, but they work well enough for me.

First thing I needed to do was copy the latest version of our ASP.NET app to every site.  To handle this, I have this:

import os # needed for traversing remote directories
import shutil # needed for file copies and deletes

sn = raw_input( "Folder Name: " ) # ask for the folder name
shutil.rmtree( "\\\\testserver\\inetpub\\webroot" + sn) # completely remove the contents of the directory named for the site
shutil.copytree( "\\\\builds\\asp\\v.1.5", "\\\\testserver\\inetpub\\webroot" + sn) # grab the latest ASPX files for the site
shutil.copytree( "\\\\builds\\bin\\v.1.7", "\\\\testserver\\inetpub\\webroot" + sn + ".wip\\bin" ) # grab the latest DLLs for the site
shutil.copyfile( "C:\\webconfigs\\" + sn + ".config", "\\\\testserver\\inetpub\\webroot" + sn + "\\web.config" ) # copy the web.config for the site and place it
print "Copied!"

"How does this do massive amounts of work?  Looks like just one site to me!"  Yes, but the Windows console is nifty in a lot of ways.  For instance, copy/pasting has some weird quirks and is perfect when you don't want to write or use a parser.  Fire it up and go to the directory the script is in:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Gh0st_Preacher>f:

F:\>cd f:\scripts

F:\Scripts>dir
Volume in drive F is Gh0stData1
Volume Serial Number is E8F9-F338

Directory of F:\Scripts

01/14/2010  11:59 PM    <DIR>          .
01/14/2010  11:59 PM    <DIR>          ..
01/14/2010  11:59 PM               697 copy.py
01/14/2010  11:59 PM             1,954 fileupdate.py
01/14/2010  11:58 PM               268 setro.py
               3 File(s)          2,919 bytes
               2 Dir(s)  583,399,571,456 bytes free

F:\Scripts>

We're working with "copy.py".  Craft a file with a list of the names of the sites you're working with, and put the script before each name, like this :
copy.py
site1
copy.py
site2
copy.py
site3

Stupid, right?  Let's see what happens when I copy/paste that into the console window that's currently in F:\scripts:

F:\scripts>copy.py
SN: site1
Copied!

F:\scripts>copy.py
SN: site2
Copied!

F:\scripts>copy.py
SN: site3
Copied!

That's the actual echo.  The windows console will wait to paste each line until it has the ability to actually place that text somewhere.  Not only that, but it'll translate carriage returns the same as you hitting "enter".  Because of this, you don't need to write a file parser for dumb scripts like this.  This copy/paste system is stupid - I am wholly aware of this - but it's simple and easy.  No extra code.  No nothin'.  Just things working.  I can't really argue with results.

So how else can this be useful?  In my case, I have to modify a value in the web.config files I copied.  The value will be the same across all sites, since we just need to point the site to a different data machine to grab our head, header, and footer files.  here's what I did (credit to TOXiC for the original here):

import fileinput, string, sys # need these for everythang

done = "n"
while done == "n":
    sn = raw_input( "sn: " )
    fileQuery = "\\\\testserver\\inetpub\\webroot\\" + sn + "\\web.config"
    sourceText = "livedata.site.com"
    replaceText = "devdata.internal.com"
    def replacemachine(fileName, sourceText, replaceText):
        file = open(fileName, "r") # open in read-only
        text = file.read() # read the file and assign to a variable
        file.close() # close out the read session
        file = open(fileName, "w") # reopen the file in write mode
        file.write(text.replace(sourceText, replaceText)) # do the replace
        # and write the new file with the correction.
        file.close() # closes the write
        print "servicemachine updated"
    replacemachine(fileQuery, sourceText, replaceText)
    done = raw_input( "done? " )

Now, this time, I decided to get crafty.  See that loop in there?  You can just keep telling it "n" for "done" and it'll start over again.  Starting at F:\scripts, copy/paste a file formatted like this:

fileupdate.py
site1
n
site2
n
site3

Still simple, still stupid, but it still works.  Output?

sn: Site1
servicemachine updated
done? n
sn: Site2
servicemachine updated
done? n
sn: Site3
servicemachine updated
done? y

And our updates are done!

We could roll both of these up into a big script, but I'm lazy.  Seriously lazy.  Copy/paste lazy.

This is an example of duct tape programming.  It's dirty, but quick and effective.

Keep it simple, kids.

0 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Whiskey and Guitars - Sounds Like A Good Holiday Season

Woo for double-content posts!

My roommates Greebo and Diieu (those are their screennames - not about to post their real names) got me a Christmas gift that blew my fucking mind away.

Yamazaki 18 Year Single Malt Whisky.

I wanted to try Suntory whisky - it was the only Whisky I hadn't tried, and it pisses me off.  That's right, it makes me angry.  They got me a bottle of Whisky, and that pisses me off.

I had consistency in my life.  Whisky/Whiskey came from Tennessee, Kentucky, Scotland, Ireland, and Canada.  Nowhere else.  At least, not the good stuff.  But Yamazaki completely changed that.  It's smooth - smoother then anything else I've ever tried before, and it's got an amazing, mature, full flavor palette.  It's damn good.

So now my Whisky/Whiskey list, in order of preference to sip, is as such:

Scotch, Suntory, Bourbon, Tennessee, Canadian, Irish.

You see that breakdown?  Suntory is now preferred over bourbon for sipping.  See how wrong that is?  It's like the softer brother of Scotch.  While I have sincere appreciation for the rough edges of Scotch - as well as the other darker flavors contained therein, Suntory breaks that ideal and goes to the smooth edge.  All the flavor, but with a bit more of a refined feel.

God dammit.

Speaking of gifts, I'm recording as a gift this year, a continuation of last year, where I recorded a 6 song CD for a Christmas present to my family and friends.  I included 3 drum and bass songs, as well as 3 songs with guitar and other instruments.  All of them but one were original compositions.

This year, I'm going it again, but all the songs are going to be full-on guitars, drums, and bass.  I'm not doing electronic music this time around because I want to do the full recording deal.  It's been a challenge so far, but fun.  My track list is coming down to something like this:

1. Delé - New Hard Rock song with delay laced intro.
2. Walk - Solo acoustic piece written for Allison's graduation.
3. Goin' Down - Cover of a cover of a Freddie King song
4. Untitled progressive song here - think Vai-ish with Satriani hanging out, too.
5. Probably a blues/rock song here.
6. Probably a acoustic guitar / electric guitar thing.  Calm outro idea.

If I can muster a 7th song in time, I'm definitely doing it, but that track list seems to be the direction I'm heading.

I've learned a lot since last year.  My rhythms are much stronger, and my melodies are a bit better - though not by much.

My recording sensibilities are a bit stronger this year, as well.  I'm making better use of the stereo soundstage to get a bigger field, and I use doubletracking quite a bit for the first song to get a bit more of a ballsy feel.

I still have a lot of work to do - I just got the drums and bass done for the third track, but overall things are going will.

I hope you're all doing well.  If I don't post before, have a wonderful holiday season!

1 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Wait, They Were Actually Good?!

Ah, it's been long, friend.  Too long.  Let me spin a tale of my travels...

Or not.

NEW PHONE

Last week I scored a new phone - the LG enV Touch.  This is a step up from my old phone - an LG enV (1st version).  Honestly?  Gigantic upgrade.  My father footed the bill on buying one for me and getting a free one for himself through Verizon's new every 2 program.  Without going into a full-blown review, this thing is awesome.  Seriously.  I have almost no complaints.  It's that good.

BB KING IN CONCERT

I was lucky enough to be able to attend the BB King Concert in town.  Amazing show.  84 years old and he still brings down the house.  He can still sing and play as well as ever.  His interactions with the audience were priceless and humorous, and the music was amazing.  I was also so lucky as to be able to get his signature on my guitar through some connections.  While I did not get to meet him, I got his freaking signature on my guitar.  I'm really excited about it.

BORDERLANDS

Okay, this came out a while ago, but we were using this is hold us over until L4D2 to came out.  It's fun, all though the replayability is questionable.  The loots are where it's at.  Not much else to it.  The difficulty was a little disappointing, however.

MODERN WAREFARE 2

I got my hands on this.  The single player is extremely well done.  While there are a few small feasibility issues contained within, it's overall extremely well done.  The controversial "airport" scene was not as bad as I thought it would be when I saw it in context.  Still terrible, but not as bad.  The ending opens it up for a 3rd game, however.

LEFT 4 DEAD 2

Okay, Valve, I get it.  You really did improve it.  But is it $50 of improvement?  I don't know.  My disappointment with this still holds out on the fact that you never really gave us the DLC you promised for L4D1.  However, I bought it as apart of the 4-pack, so I only payed $35 for it in the end.  With that aside, this is my new favorite Valve game.  Really.

FINIS

I've got dinner ready, but I will hopefully return with more content soon!  I hope, at least.  I'll give a fuller review of L4D2 when I've gotten through the campaign and all the game modes.

Until next time!

0 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

This is a phone test!

So I got a new phone. I've been testing it out, and so far, I really like it. It's the LG enV Touch. and it freakin' rocks. This is a test post using the device. Word.

1 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

The Power Hour

I bought a bunch of PBR to tackle one of my favorite drinking games.

The Power Hour.

For the uninitiated, a Power Hour one shot of beer every minute for an hour.

You can see where the good idea part comes in.

I was searching around for a good clock of some sort to give the 60 second countdowns for each minute, but the Internet failed me.  There were a lot of bad ones out there, so I made my own!

http://gh0stpreacher.com/media/powerhour/launch.html

Click the button, and you'll get a small window that displays a 60 second countdown timer, along with a handy clock to help you see the time.

I might change it later.

Woop!

0 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Okay, so we're back from that interruption

Note to self:

Don't turn on VPN when remoting.  DURP.

0 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

More Site Updates

I forgot to mention that I made more updates to the site's CSS.

I made the template WAY too wide.  Impractically so.  I reduced it by a few hundred pixels, which seems to better suit the content.  I might set a maximum width of around 850px and then make it fluid if the viewport is smaller then that.  Maybe.

I also added borders to separate out the right rail from the main content, and posts from each other.  This seems to make things feel a little more organized.

I still have a desire to add more things to the right rail, but I feel like the Twitter feed is enough for now.  One of these days I'll go back through and tag all of my posts and add a tag cloud, as well as links to my personal favorite posts.

1 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Saturday Night? Yes Please.

Hello, internets.

It is a Saturday.  That means the following things will take place:

- Liquor
- Games
- Liquor
- Food
- Caffeine
- Liquor
- More games
- Liquor

As you can see, I have a terribly busy schedule on my weekends.

Also, Thnikkalodge.

1 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

CSS Updates

My terrible ability to design is showing once again in the latest design update!

The large majority of modifications are to the site's CSS.  I still haven't had the desire to go through and actually fix up the core stylesheet's poor organization and overall bloatedness.

I also removed a few things.  The calendar in the upper-right is no longer there, as well as the single ad call.  Since I don't really get any traffic, the ad was totally unnecessary.  And the calendar was totally unneeded because I don't post often enough to necessitate it.  Also, it provided a path to a redundant post listing page that was all ready covered by the Blog Archive.

The page is much wider.  I designed it for 1000px width.

I'm not 100% pleased with the new design, but it's much better than the template before.  Hopefully this one is actually readable, which is the biggest complaint I received about the last template.

The color scheme was driven by my desire to not use as little pure white and black as possible.  It's composed of blues and greys, which turned out well, mostly.

More updates will come, but for now, I'm happy.

Until next time.

2 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

THE INTERNETS IS DANGEROUS OMG

Ever since I got quasi-serious about this serverbox, I've been keeping track of a few internet security sites.  They all provide various perspectives to internet security and give a unique look into the state of OMG T3H HAXX0RZ N VIRUSESES.

Many of these sites get at the same point - the internet is insecure in so many ways.  Why don't common folk work to make the effort to learn about security?!  I've found myself of that side of the fence a few times.  "People need to learn and englighten themselves!  They need not live within the dark ages any more!"

But then I thought about all those other industries and how those industries don't expect the same of their "users".

It used to be that, back in ye olde days, if your watch broke, you went to a jeweller and/or watchmaker and had them fix it.  All you knew was that there's a part of it that tells you what time it is, that it needs to be wound up sometimes, and that the watch should tick.  Jewellers didn't berate customers because they didn't know the internals of the watch.  They did the work because, hey, that's their job.

I could point to any number of industries that follows this general rule.  But I think the exceptional embitterment within IT folks is one that's actually quite justified.

See, just as there's the jeweller or watchmaker who'd repair watches, they wouldn't do it for free.  They might give a discounted rate, but it's rude to go to a jeweller friend, give them your watch, and go "This is broken.  Fix?"  This behavior is seen within the IT industry.  Instead, people will call you up and go "Hey, the thing won't go.  How do I make it go?"

This is a poor way to treat that friend of yours who's a "computer person" because, if they work in IT, they've been doing that all day.  You're offering them no incentive to help you out, and giving them plenty of reason to haul off and punch you.

I think this stems from the fact that computer people don't make things that you can hold.  We don't have any sort of metric for our work other then "There is now a website" or "The connection between this production machine and this backend database is now 5% faster".

So how do we fix both ends of this problem?  How do we address the problem of helping users help themselves, and how do we get users to understand that your expertise isn't always free?  Both of these could be tackled any number of ways, but I think it comes down to each side flexing a little bit.

First and foremost, the IT industry needs to make an effort to at least somewhat simplify it's messages about security.  The same stance has been taken for years, and it obviously hasn't worked.  We need to help users understand what the SMB Exploit in Windows 7 means to them.  Remember when the automotive industry had to recall tires on Ford Explorers all those years ago?  All they needed to say was "the tires could explode and the car could crash".  That's it.  People understood that.  Telling people that the SMB exploit could allow remote code execution is a terrible way to assist them in understanding the real problem, which is that remote code execution could mean theft or loss of personal information.  That's talk people understand.

I take my stance here, again, based on observing other industries.  When an electronics company does a recall on stereo amplifiers, they don't say "We recieved a batch of capacitors that under some circumstances generate excessive heat", they say "There is a fire hazard related to some internal components".  People understand that better.

To help users understand why your expertise shouldn't be assumed "free", there needs to be a serious discussion that we are just like watchmakers, automotive engineers, and steel mill workers.  We do what we do because, while we hopefully enjoy it, we need to earn a buck, too.  People need to stop assuming we are always available as tech support.  We'll be there when we can, but let us not be there when we want.

Computers are commonplace enough to the point that I think we ought not hold out any more.  Both sides need to come together and work to understand each other better.  Once we do that, the industry will become a much happier place.

0 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed